【发布时间】:2014-04-04 18:39:12
【问题描述】:
我在一个页面中有这个 html 代码
<tr class="tt_row">
<td class="ttr_type"><a href="?cat=17"><img src="/pic/abc-aaa-bb.png" alt="aa/bb-cccc" /></a></td>
<td class="ttr_name"><a href="abc?id=2221" title="Test.2123.123"><b>Test.2123.123</b></a><br /><span class="pre">Test test</span> <span class="newtag">NEW!</span></td>
<td class="td_dl"><a href="upload/222083/1348hgfhfchf5675675/Test.2123.123"><img src="/pic/aaab.gif" style="vertical-align: middle;" alt="Upload" /></a></td>
<td class="ttr_size">1.65 MB<br /><a class="small" href="abc?id=2221&filelist=1#filelist">15 Files</a></td>
<td class="ttr_comments">0</td>
<td class="ttr_added">2014-03-03<br />11:11:11</td>
<td class="ttr_snatched">0<br />times</td>
<td class="ttr_seeders"><b><a href="abc?id=2221&peers=1#seeders">0</a></b></td>
<td class="ttr_leechers">0</td>
</tr>
还有这个带有 Greasemonkey 的脚本
function initMenu(aEvent) {
// Executed when user right click on web page body
// aEvent.target is the element you right click on
var node = aEvent.target.parentNode;
var node1 = aEvent.target;
var item = document.querySelector("#userscript-search-by-image menuitem");
if (node.localName == "a") {
if (node1.getAttribute("src") == "/pic/aaab.gif") { // ABV
body.setAttribute("contextmenu", "userscript-search-by-image");
item.setAttribute("imageURL", node.href);
}
当我右键单击时,此脚本会使用新条目修改上下文菜单
<img src="/pic/aaab.gif" style="vertical-align: middle;" alt="Upload" />
和 parentNode 我去
<a href="upload/222083/1348hgfhfchf5675675/Test.2123.123">
并用
保存hrefitem.setAttribute("imageURL", node.href);
我不能做的是保存 alt
<img src="/pic/abc-aaa-bb.png" alt="aa/bb-cccc" />
我试过了
node3 = node.parentNode.parentNode.firstChild.firstChild.firstChild
到达
<img src="/pic/abc-aaa-bb.png" alt="aa/bb-cccc" />
和
item.setAttribute("imageALT", node3.alt);
我该怎么办?我是 javascript 和 DOM 的新手。
【问题讨论】:
-
有一个显式的
if条件只在点击图片.getAttribute("src") == "/pic/aaab.gif"时才做某些动作……它与parentNode无关。
标签: javascript dom greasemonkey