获取元素的html标记内容(Get element's html tag content)
是否可以通过dom api获取节点的顶级标签html? 要清楚,如果我有
<div data-x="a"> <span>Hello</span> </div>
我想回到
<div data-x="a">
在outerHTML上匹配的原始字符串是我能做的最好的,还是有快速直接的方法来实现我想要的?
Is it possible to get a node's top-level tag html via the dom api? To be clear, if I have
<div data-x="a"> <span>Hello</span> </div>
I want to just get back
<div data-x="a">
Is a crude string matching on outerHTML the best I can do, or is there a fast and direct way to achieve what I want?
更新时间:2021-12-17 19:12
最满意答案
如果克隆节点,则innerHTML属性将为空。 对于您的示例,浅克隆是合适的(传递
false
或不传递任何东西)。// get the div element var element = document.querySelectorAll('div')[0]; // view the outerHTML of the element console.log('original outerHTML', element.outerHTML); // clone the element var clone = element.cloneNode(); // view the outerHTML of the clone console.log('outerHTML of clone', clone.outerHTML); // has what you want
<div data-x="a"> <span>Hello</span> </div>
If you clone the node, the innerHTML property will be empty. For your example, a shallow clone is appropriate (pass
false
or don't pass anything).// get the div element var element = document.querySelectorAll('div')[0]; // view the outerHTML of the element console.log('original outerHTML', element.outerHTML); // clone the element var clone = element.cloneNode(); // view the outerHTML of the clone console.log('outerHTML of clone', clone.outerHTML); // has what you want
<div data-x="a"> <span>Hello</span> </div>
相关问答
更多-
按内容删除HTML标记(remove HTML tag by content)[2022-06-16]
试试这个 loadHTML($reportGen); $xpath = new DOMXpath($domDoc); $tags = $xpath->query('//td'); foreach($tags as $tag) { $value = $tag->nodeValue; if(preg_match('/^(Â )/',$value)) ... -
通常,您可以将client[Height|Width]与scroll[Height|Width] ,以便检测到...但是当溢出可见时,值将相同。 因此,检测程序必须说明这一点: // Determines if the passed element is overflowing its bounds, // either vertically or horizontally. // Will temporarily modify the "overflow" style to detect this // ...
-
您可以使用html()函数获取每个li的html内容,然后搜索它是否包含所需的文本:“hideme”。 $("li").each(function () { if ($(this).html().indexOf('hideme') > -1) { $(this).hide(); } }); 演示 You can use the html() function to get the html contents of each li and then search if it ...
-
您尝试使用的代码(即render :update do |page|等)用于更新HTML页面(例如page['content'].innerHTML = 'Thank you, we received your data.' ),而不是检索来自页面的数据(必须在表单提交中发生)。 选项1)只需使用已提交的表单字段数据。 (你没有包含一个实际的例子,我不知道你是否还有其他非格式内容要保存。)我倾向于通过将params散列转换为JSON字符串并将其保存在数据库中的文本字段。 例如@evaluation.cont ...
-
替代HTML5中的
标签?(Alternative to [2022-01-23]tag in HTML5?) 相同的文档( Mozilla FireFox )声明: 它现在已被元素取代。 使用 元素。 当某些东西被弃用时,你不应该再使用它了。 ( Mozilla也说明了这一点。)弃用意味着它不再被批准/不批准。 The same documentation (Mozilla FireFox) states: It has now been replaced by the element. Use the element. When something is depr ... -
如果您对字符串的任何部分不区分大小写匹配感到满意,则可以使用:contains : $('#nameMachine h3:contains("XX")'); 或者,如果您要求字符串的完全匹配区分大小写,则可以使用filter() : $('#nameMachine h3').filter(function() { return $(this).text().trim() == 'XX'; }) If you're happy with a case insensitive match on any ...
-
获取元素的html标记内容(Get element's html tag content)[2021-12-17]
如果克隆节点,则innerHTML属性将为空。 对于您的示例,浅克隆是合适的(传递false或不传递任何东西)。 // get the div element var element = document.querySelectorAll('div')[0]; // view the outerHTML of the element console.log('original outerHTML', element.outerHTML); // clone the element var ... -
您可以使用PHP Simple HTML DOM解析器来解析HTML,如DOMDocument for XML。 注意: PHP也直接支持DOMDocument 。 $scrape_address = "http://www.al-madina.com/node/444862"; $ch = curl_init($scrape_address); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, '1'); curl_setopt($ch, ...
-
如何从伪元素内容中插入HTML标记:[复制](How to inserted HTML tag from pseudo-element content: [duplicate])[2021-05-20]
您不能使用CSS伪元素的content属性注入HTML元素; 但是,您可以将它们设置为display: block和(在您的问题中似乎需要,但不是您演示/尝试过的代码)使用height和background-color来模拟
: article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; } article::befo ... -
Selenium API不支持注释节点。 但是你可以通过这段JavaScript轻松获得评论: head = driver.find_element_by_css_selector("head") comment = get_element_comment(head) print(comment) def get_element_comment(element): return element._parent.execute_script(""" return Array.protot ...