简体   繁体   English

如何获取TAG值/文本(如innerHTML)?

[英]How to get TAG value/text (like innerHTML)?

How to get TAG value (not innerHTML)? 如何获得TAG值(不是innerHTML)? Lets supose i have a... link or image. 让我们假设我有一个...链接或图像。

<link href="bla"> | <img src="ble" />

let suppose there is only 1 link in a document: 假设文档中只有1个链接:

var links = document.link;

how to get TAG text as: ' <link href="bla"> ' by JS (cause innerHTML is empty cause there is not text INSIDE tag, cause it does not have ending >)? 如何通过JS获得TAG文本:' <link href="bla"> '(由于innerHTML为空,因为没有文本INSIDE标签,因为它没有结尾>)?

You can use the DOM element attribute outerHTML 您可以使用DOM元素属性externalHTML

$(selector)[0].outerHTML

which gets the first DOM element of the selection and then acquires the html using the DOM attribute outerHTML 它获取选择的第一个DOM元素,然后使用DOM属性externalHTML获取html

If you do not want the content but only the enclosing tag you could do this 如果您不想要内容,而只想要封闭标签,则可以这样做

$.fn.tag = function(){
    return this[0].outerHTML.replace(this.html(),"");
};

问题尚不完全清楚,但是我希望您想要属性值

var href = $(link).attr('href');

您需要做的就是这个(纯JS):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM