从中调用函数的链接的id [英] id of a link that a function is called from
问题描述
我希望在这里发布更具体的代码不是问题,但我认为如果每个人都能看到它会更好地解释,所以我会给你我的代码然后我会解释我的问题。
I hope it's not a problem to post much specific code here, but I figure it will be better explained if everyone can just see it, so I will give you my code and then I will explain my problem.
我的代码:
function addBeGoneLinks () {
var beGoneClassElems;
var beGoneSpan;
var beGoneLink;
var beGonePrintSafe;
var spacesSpan;
//var middotSpan = document.createElement ('span');
var interactionContainer = document.getElementsByClassName('feedItemInteractionContainer');
for (var i=0; i<children.length; i++)
{
beGonePrintSafe = false;
beGoneClassElems = children[i].getElementsByClassName('beGone')
beGonePrintSafe = true;
if (beGoneClassElems.length == 0)
{
beGoneLink = document.createElement('a');
beGoneLink.href = 'javascript:void(0);';
beGoneLink.appendChild(document.createTextNode('Be Gone'));
beGoneLink.className = 'beGone';
beGoneLink.id = 'beGoneLink' + i.toString();
beGoneLink.addEventListener ("click", function() {beGone();}, false);//This line!
beGoneLink.align = 'right';
spacesSpan = document.createElement('span');
spacesSpan.innerHTML = ' - ';
if (interactionContainer[i] != undefined)
{
interactionContainer[i].appendChild(spacesSpan);
interactionContainer[i].appendChild(beGoneLink);
}
}
}
}
这里我有一个我正在研究的Greasemonkey脚本的函数。当点击其中一个链接时,我的目标是让它调用函数 beGone()
除其他外,将删除几个父母的整个元素,从而移除他们的兄弟姐妹,他们的父母和他们的父母的兄弟姐妹,以及之后的一两个级别。
Here I have a function from a Greasemonkey script that I am working on. When one of the links is clicked, my aim is to have it call the function beGone()
which will, among other things, remove the whole element a few parents up, thereby removing their sibling's, their parents and their parents' siblings, and one or two levels after that.
我的想法只是获取被按下的链接的ID将它传递给 beGone()
,这样我就可以使用它的id获取父母,但我不知道该怎么做。我能够通过它调用的函数传递一个链接的id吗?如果没有,有没有其他方法可以做到这一点?
My idea was just to get the id of the link that was pressed and pass it to beGone()
so that I could then get the parents using its id, but I do not know how to do that. Am I able to have the id of a link passed by the function that it calls? If not, is there any other way to do this?
我不确定我是否缺少一些非常简单的解决方案,但我找不到一个扎根于网络,特别是因为我不确定如何搜索这个特定的问题。
I am not sure whether I am missing some really simple solution, but I haven't been able to find one rooting around the web, especially because I was unsure how I would search for this specific problem.
推荐答案
试试这个:
beGoneLink.addEventListener("click", beGone, false);
beGone = function (evt) {
evt.target; // evt.target refers to the clicked element.
...
}
你可以然后使用 evt.target.id
, evt.target.parentNode
等。
这篇关于从中调用函数的链接的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!