XUL 将事件附加到使用 javascript 添加的锚点
我正在编写一个 Firefox 扩展,它将事件侦听器添加到页面上的所有锚标记。基本上我有:
window.addEventListener("load", function() { myExtension.init(); }, false);
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent");
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
},
onPageLoad: function(event) {
var doc = event.originalTarget;
var anchors = doc.getElementsByTagName("a");
//attach event listeners
}
}
这适用于页面上的大多数标签。问题是用 javascript 添加的标签不会附加事件侦听器。我尝试过重新定义 createElement:
//in the onPageLoadFunction
var originalCreateElement = doc.createElement;
doc.createElement = function(tag) {
if (tag != "a"){
return originalCreateElement(tag);
}
var anchor = originalCreateElement("a");
anchor.addEventListener("mouseover", myInterestingFunction, false);
return anchor;
}
并尝试添加 DOM 插入侦听器
//in the onPageLoadFunction
function nodeInserted(event){;
addToChildren(event.originalTarget);
}
function addToChildren(node){
if (node.hasChildNodes()){
var nodes = node.childNodes;
for (var i = 0; i < nodes.length; i++){
addToChildren(nodes[i]);
}
}
if (node.nodeName == "a"){
anchorEvent(node); //adds event listeners to node
}
}
doc.addEventListener("DOMNodeInserted", nodeInserted, false);
,但都不起作用。如何获取对这些锚点对象的引用,以便向它们添加侦听器?
谢谢
I'm writing a firefox extension that adds event listeners to all the anchor tags on a page. Essentially I have:
window.addEventListener("load", function() { myExtension.init(); }, false);
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent");
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
},
onPageLoad: function(event) {
var doc = event.originalTarget;
var anchors = doc.getElementsByTagName("a");
//attach event listeners
}
}
This works for most of the tags on the page. The problem is that tags added with javascript don't get the event listener attached. I've tried redefining createElement:
//in the onPageLoadFunction
var originalCreateElement = doc.createElement;
doc.createElement = function(tag) {
if (tag != "a"){
return originalCreateElement(tag);
}
var anchor = originalCreateElement("a");
anchor.addEventListener("mouseover", myInterestingFunction, false);
return anchor;
}
and I've tried adding a DOM insertion listener
//in the onPageLoadFunction
function nodeInserted(event){;
addToChildren(event.originalTarget);
}
function addToChildren(node){
if (node.hasChildNodes()){
var nodes = node.childNodes;
for (var i = 0; i < nodes.length; i++){
addToChildren(nodes[i]);
}
}
if (node.nodeName == "a"){
anchorEvent(node); //adds event listeners to node
}
}
doc.addEventListener("DOMNodeInserted", nodeInserted, false);
but neither work. How can I get references to these anchor objects so I can add listeners to them?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态添加“事件侦听器到页面上的所有锚标记”很难有效地完成。您有两种选择:
jQuery.live()
所做的那样)前者对性能影响明显。使用突变事件会带来隐藏的性能成本:如果为文档添加任何突变事件,Gecko 在其 DOM 代码中会采用较慢的路径,因此所有 DOM 操作都会变慢。
为什么不使用 addEventListener 在某些顶级元素(甚至整个窗口)上设置侦听器并检查目标是否是锚点?您可以使用捕获侦听器来使您的侦听器在默认操作发生之前被调用。
Adding "event listeners to all the anchor tags on a page" dynamically is hard to do efficiently. You have two choices:
jQuery.live()
does)The former has an obvious performance impact. Using mutation events has a hidden performance cost: if any mutation events are added for a document, Gecko takes a slower path in its DOM code, so all DOM manipulation works slower.
Why not use
addEventListener
to set up a listener on some top-level element (or even the whole window) and check if the target is an anchor? You could use a capturing listener to make your listener be called before the default action occurs.