HTMLElement를 문자열로 변환하는 방법
서버 측과 데이터를 교환하기 위해 JavaScript에서 XML 요소를 만들 것입니다. 나는 그것을 할 수 있다는 것을 알았다 document.createElement. 그러나 나는 그것을 문자열로 변환하는 방법을 모릅니다. 브라우저에 쉽게 사용할 수있는 API가 있습니까? 아니면이 API를 포함하는 JS 라이브러리가 있습니까?
편집 : 브라우저 API XMLSerializer를 찾았습니다. 문자열로 직렬화하는 올바른 방법이어야합니다.
요소를 복제하고 비어있는 'offstage'컨테이너에 추가하고 컨테이너의 innerHTML을 읽어 'outer-html'을 얻을 수 있습니다.
이 예제는 선택적 두 번째 매개 변수를 사용합니다.
요소의 하위 항목을 포함하려면 document.getHTML (element, true)를 호출하십시오.
document.getHTML= function(who, deep){
if(!who || !who.tagName) return '';
var txt, ax, el= document.createElement("div");
el.appendChild(who.cloneNode(false));
txt= el.innerHTML;
if(deep){
ax= txt.indexOf('>')+1;
txt= txt.substring(0, ax)+who.innerHTML+ txt.substring(ax);
}
el= null;
return txt;
}
요소 outerHTML속성 (참고 : 버전 11 이후 Firefox에서 지원됨 )은 전체 요소의 HTML을 반환합니다.
예
<div id="new-element-1">Hello world.</div>
<script type="text/javascript"><!--
var element = document.getElementById("new-element-1");
var elementHtml = element.outerHTML;
// <div id="new-element-1">Hello world.</div>
--></script>
마찬가지로을 사용 innerHTML하여 주어진 요소에 포함 된 HTML innerText을 가져 오거나 요소 내부에 텍스트를 가져올 수 있습니다 (HTML 마크 업 제외).
또한보십시오
가장 쉬운 방법은 해당 요소의 innerHTML을 tmp 변수에 복사하고 비운 다음 새 요소를 추가 한 다음 tmp 변수를 다시 복사하는 것입니다. 다음은 헤드 상단에 jquery 스크립트를 추가하는 데 사용한 예입니다.
var imported = document.createElement('script');
imported.src = 'http://code.jquery.com/jquery-1.7.1.js';
var tmpHead = document.head.innerHTML;
document.head.innerHTML = "";
document.head.append(imported);
document.head.innerHTML += tmpHead;
그 간단한 :)
있다 tagName재산 및 attributes과 재산은 :
var element = document.getElementById("wtv");
var openTag = "<"+element.tagName;
for (var i = 0; i < element.attributes.length; i++) {
var attrib = element.attributes[i];
openTag += " "+attrib.name + "=" + attrib.value;
}
openTag += ">";
alert(openTag);
HTML 요소의 모든 속성을 반복하는 방법 도 참조하십시오 . (내가 했어!)
To get the contents between the open and close tags you could probably use innerHTML if you don't want to iterate over all the child elements...
alert(element.innerHTML);
... and then get the close tag again with tagName.
var closeTag = "</"+element.tagName+">";
alert(closeTag);
This might not apply to everyone case, but when extracting from xml i had this problem, which i solved with this.
function grab_xml(what){
var return_xml =null;
$.ajax({
type: "GET",
url: what,
success:function(xml){return_xml =xml;},
async: false
});
return(return_xml);
}
then get the xml:
var sector_xml=grab_xml("p/sector.xml");
var tt=$(sector_xml).find("pt");
Then I then made this function to extract xml line , when i need to read from an XML file, containing html tags.
function extract_xml_line(who){
var tmp = document.createElement("div");
tmp.appendChild(who[0]);
var tmp=$(tmp.innerHTML).html();
return(tmp);
}
and now to conclude:
var str_of_html= extract_xml_line(tt.find("intro")); //outputs the intro tag and whats inside it: helllo <b>in bold</b>
참고URL : https://stackoverflow.com/questions/2474605/how-to-convert-a-htmlelement-to-a-string
'Nice programing' 카테고리의 다른 글
| 관용 부호 란 무엇입니까? (0) | 2020.11.07 |
|---|---|
| 모든 하위 요소에 대한 CLASS 제거 (0) | 2020.11.07 |
| Python : 한 세트에 다른 세트가 완전히 포함되어 있는지 확인 하시겠습니까? (0) | 2020.11.07 |
| Git은 파일을 어디에 저장합니까? (0) | 2020.11.07 |
| NSARC의 객체에 대한 약한 참조 (__unsafe_unretained) 배열 (0) | 2020.11.06 |