programing

JavaScript / jQuery에서 HTML 태그를 얻는 방법

nicegoodjob 2023. 1. 6. 20:14
반응형

JavaScript / jQuery에서 HTML 태그를 얻는 방법

사용.$('html').html()html을 취득할 수 있습니다.<html>태그( )<head>,<body>등).하지만 어떻게 하면 실제 HTML을 얻을 수 있을까요?<html>태그(속성 포함)를 지정하시겠습니까?

또는 페이지의 전체 HTML(doctpe,<html>jQuery(또는 플레인오래된 JavaScript)를 사용하는 경우 등).

가장 간단한 방법으로html요소는 기본적으로 다음과 같습니다.

document.documentElement

다음은 참고 자료입니다. https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement

업데이트: 그 다음에html요소를 문자열로 지정합니다.

document.documentElement.outerHTML

다음은 순수하게 JS를 사용하여html DOM 요소를 얻는 방법입니다.

var htmlElement = document.getElementsByTagName("html")[0];

또는

var htmlElement = document.querySelector("html");

jQuery를 사용하여 속성을 가져오려면...

$(htmlElement).attr(INSERT-ATTRIBUTE-NAME);

다른 답변 외에 다음 방법으로 HTML 요소에 액세스할 수도 있습니다.

var htmlEl = document.body.parentNode;

그러면 내부 HTML 콘텐츠를 얻을 수 있습니다.

var inner = htmlEl.innerHTML;

이렇게 하는 것이 조금 더 빠른 것 같습니다.다만, HTML 요소를 취득하고 있는 경우는,document.body.parentNode꽤 빠른 것 같아요.

HTML 요소가 있으면및 메서드를 사용하여 Atribute를 조작할 수 있습니다.

DOSCTYPE의 경우 이 질문에 자세히 설명되어 있는 를 사용할 수 있습니다.

jQuery의 경우:

var html_string = $('html').outerHTML()

일반 Javascript:

var html_string = document.documentElement.outerHTML

jQuery를 사용하여 HTML 요소의 속성을 가져오려면.attr();

그렇게$('html').attr('someAttribute');의 가치를 부여해 드립니다.someAttribute자연의html

http://api.jquery.com/attr/

기타:

jQuery 플러그인은 다음과 같습니다.http://plugins.jquery.com/project/getAttributes

HTML 요소에서 모든 속성을 가져올 수 있습니다.

플레인 javascript를 사용하여 다른 간단한 방법을 찾았습니다.

const htmlElement = document.querySelector(':root')
console.log(htmlElement)

언급URL : https://stackoverflow.com/questions/4196971/how-to-get-the-html-tag-html-with-javascript-jquery

반응형