admin管理员组文章数量:1427321
I'm trying to write a greasemonkey script that only displays photos with the tags bacon.
The site it runs on is written like so:
<div class="photos">
<ul>
...
<li>
<a href="photo1"> <img src=".jpg" </a> <br /> <a href="tags_photo1"> <span class="tags"> bacon, delicious </span> </a>
</li>
...
</ul>
</div>
At first I tried using DOM by acessing div and then using childNodes. I could access the img and both href nodes, but not span.
Next I tried using this to get the tags from the span:
tagNodes=document.getElementsByClassName('tags');
And it returned a XPCNativeWrapper collection all of whose elements were undefined.
Any ideas as to how to get at the tags?I'm fairly new to javascript, so I'm sorry if my question is stupid.
[Edit]
var spans, tags;
spans = document.getElementsByTagName('span');
for (var i = spans.length - 1; i >= 0; --i)
{
tags = spans[i];
alert(tags.wrappedJSObject.nodeValue);
}
Returns as null, even with wrappedJSObject. Is it because Object.prototype doesn't work for XPCNativeWrapper? Or am I missing something?
I'm trying to write a greasemonkey script that only displays photos with the tags bacon.
The site it runs on is written like so:
<div class="photos">
<ul>
...
<li>
<a href="photo1"> <img src="http://somesite.co/photo1.jpg" </a> <br /> <a href="tags_photo1"> <span class="tags"> bacon, delicious </span> </a>
</li>
...
</ul>
</div>
At first I tried using DOM by acessing div and then using childNodes. I could access the img and both href nodes, but not span.
Next I tried using this to get the tags from the span:
tagNodes=document.getElementsByClassName('tags');
And it returned a XPCNativeWrapper collection all of whose elements were undefined.
Any ideas as to how to get at the tags?I'm fairly new to javascript, so I'm sorry if my question is stupid.
[Edit]
var spans, tags;
spans = document.getElementsByTagName('span');
for (var i = spans.length - 1; i >= 0; --i)
{
tags = spans[i];
alert(tags.wrappedJSObject.nodeValue);
}
Returns as null, even with wrappedJSObject. Is it because Object.prototype doesn't work for XPCNativeWrapper? Or am I missing something?
Share Improve this question edited Aug 4, 2010 at 3:10 Vcitric asked Aug 4, 2010 at 0:47 VcitricVcitric 451 silver badge6 bronze badges1 Answer
Reset to default 3Using pure Javascript (rather than using a library like jQuery - which I don't think you can use in a greasemonkey script), this should work:
var spans = document.getElementsByTagName("span");
for(var i = spans.length - 1; i >= 0; i--) {
if(spans[i].className == "tags") {
var span = spans[i];
// do something to span
}
}
You might want to look into using jQuery, if you can, because the code in jQuery would be:
$("span.tags").each(function() {
var span = this;
// do something to span
});
[EDIT]
You might be having problems because your img tag isn't closed.
Once you get access to the span with the tags in it, you just need to get it's innerHTML
property.
This code will remove the whole list item, if the tags element doesn't contain "bacon":
var spans = document.getElementsByTagName("span");
for(var i = spans.length - 1; i >= 0; i--) {
if(spans[i].className == "tags") {
var span = spans[i];
if (!span.innerHTML.match(/bacon/i)) {
var li = span.parentElement.parentElement;
li.style.display = "none";
}
}
}
本文标签: domAccessing ltspangt node in JavascriptStack Overflow
版权声明:本文标题:dom - Accessing <span> node in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745495400a2660784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论