简体   繁体   English

如何从javascript srtring的标签中获取ID或属性值

[英]how to get id or attribute value from a tag from javascript srtring

I am trying to get the id or attribute value of a tag from javascript variable ie all the html content are in a javascript variable. 我试图从javascript变量中获取标记的id或属性值,即所有html内容都在javascript变量中。 So i am not able to get the id of a perticular tag from the javascript variable. 因此,我无法从javascript变量中获取垂直标记的ID。

Here is my code sample. 这是我的代码示例。

var v="<html><head></head><body id='htmlBody' ></body></html>"
var chk = $(v);
alert($(chk).find("body").attr('id'));

Thanks, Dipesh 谢谢Dipesh

This is an interesting one. 这是一个有趣的。 It doesn't seem to recognise exactly any of those elements (html, head, body). 它似乎无法完全识别所有这些元素(html,head,body)。 I tried adding a title element into the head it recognises that and I tried adding ap element into the body and it recognises that too. 我尝试将title元素添加到头部,它可以识别出该内容,并且尝试将ap元素添加到正文中,并且也可以识别出该内容。

You might only be able to get this with a regular expression? 您可能只能使用正则表达式来获得它?

var v="<html><head></head><body id='htmlBody'></body></html>"
var matches = v.match(/body id='(.*?)'/i);
alert(matches[1]);

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

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