Javascript单击事件未触发标记
这基本上是自动点击按钮来接受/拒绝facebook中的所有请求。JavaScript不支持点击锚(Javascript单击事件未触发标记,javascript,Javascript,这基本上是自动点击按钮来接受/拒绝facebook中的所有请求。JavaScript不支持点击锚(标记)。但是,有一个解决方法:使用一些自定义JavaScript代码,您可以模拟对锚的单击 这是我创建的一个小片段。它模拟您预期的操作,即使您使用目标属性。我做了一个检查,看看这个方法是否真的在锚上运行,所以你甚至可以在你的按钮上使用这个方法,期待正常的行为。您只需将其添加到页面的某个位置即可。我在Safari 5中进行了测试 javascript:document.getElementById("
标记)。但是,有一个解决方法:使用一些自定义JavaScript代码,您可以模拟对锚的单击
这是我创建的一个小片段。它模拟您预期的操作,即使您使用目标属性。我做了一个检查,看看这个方法是否真的在锚上运行,所以你甚至可以在你的按钮上使用这个方法,期待正常的行为。您只需将其添加到页面的某个位置即可。我在Safari 5中进行了测试
javascript:document.getElementById("button").click(); //working find
javascript:document.getElementsByTagName("a")[0].click(); //not working, Why
您可以这样使用代码段:
Element.prototype.anchorClick = function() {
if (this.click) return this.click();
if (this.onclick) { var result = this.onclick(); if (!result) return result; }
switch (this.target) {
case '_blank': window.open(this.href); break;
case '_parent': parent.location = this.href; break;
case '_top': top.location = this.href; break;
case '_self': case '': window.location = this.href; break;
default: if (parent[this.target]) parent[this.target].location = this.href; else window.open(this.href); break;
}
return true;
}
以下是完整版本,其中包含一些注释:
javascript:document.getElementById('anchor').anchorClick();
javascript:document.getElementsByTagName('a')[0].anchorClick();
onclick
在Chrome和Firefox3.6中为我工作:
Element.prototype.anchorClick = function() {
// If there's a click method, the element isn't an anchor
if (this.click) {
// Just run the click method instead
return this.click();
}
// Is there an onclick method?
if (this.onclick) {
// Run the method and get the result
var result = this.onclick();
// Isn't the result true?
if (!result) {
// Cancel the operation and return the result
return result;
}
}
// Check the target property
switch (this.target) {
// _blank means a new window
case '_blank':
// Open the window
window.open(this.href);
break;
// _parent means the parent frame
case '_parent':
parent.location = this.href;
break;
// _top means the top frame
case '_top':
top.location = this.href;
break;
// _self means the current frame
// When there's no value for the target property, this is the expected result
case '_self':
case '':
window.location = this.href;
break;
// The last option is a user specified frame
default:
// Does the frame actually exist?
if (parent[this.target]) {
// Yep, and we'll open the page in that frame
parent[this.target].location = this.href;
} else {
// Nope, the frame doesn't exist
// The expected behaviour (in Safari) is opening a new window
window.open(this.href);
}
break;
}
return true;
}
Element.prototype.anchorClick = function() {
// If there's a click method, the element isn't an anchor
if (this.click) {
// Just run the click method instead
return this.click();
}
// Is there an onclick method?
if (this.onclick) {
// Run the method and get the result
var result = this.onclick();
// Isn't the result true?
if (!result) {
// Cancel the operation and return the result
return result;
}
}
// Check the target property
switch (this.target) {
// _blank means a new window
case '_blank':
// Open the window
window.open(this.href);
break;
// _parent means the parent frame
case '_parent':
parent.location = this.href;
break;
// _top means the top frame
case '_top':
top.location = this.href;
break;
// _self means the current frame
// When there's no value for the target property, this is the expected result
case '_self':
case '':
window.location = this.href;
break;
// The last option is a user specified frame
default:
// Does the frame actually exist?
if (parent[this.target]) {
// Yep, and we'll open the page in that frame
parent[this.target].location = this.href;
} else {
// Nope, the frame doesn't exist
// The expected behaviour (in Safari) is opening a new window
window.open(this.href);
}
break;
}
return true;
}
javascript:document.getElementsByTagName("a")[0].onclick();