尝试在 html 中选择以下链接:
<a href="/node/1742957/nodequeue">Nodequeue</a>
使用这个 JavaScript:
jQuery("a:contains('Nodequeue')").trigger("click");
我收到此错误消息:
Javascript console (:1): Unsafe JavaScript attempt to access frame with URL http://cdn.nprove.com/cpma/p/1/2/e/b/12ebf3bc368ry3ra.html?npuid=1310010225&rurl=&id=cpma-2n7eypbvio581300288437193&null=&r=366424962878227 from frame with URL http://www.benzinga.com/analyst-ratings/analyst-color/11/07/1742957/the-beef-stops-here. Domains, protocols and ports must match.
知道什么可能会导致这种情况吗?
最佳答案
我创建了一个 JSFiddle of your code您可以查看并注意到在控制台中您的错误不会在 Chrome 12 或 FireFox 5 中出现。我不确定您使用的 jQuery 版本是什么导致了该错误,或者您的 DOM 情况可能会触发该错误但是,错误,请尝试这个潜在的修复:
(function(window, $) {
$.fn.triggerAnchor = function() {
return this.each(function(e) {
var href = $(this).attr('href');
window.location.href = href;
return false;
});
};
})(this, this.jQuery);
然后使用:
$("a:contains('Nodequeue')").triggerAnchor();
我不认为 jQuery 会触发 anchor ,它当然也不会触发 native 点击事件。这是我能想到的最接近模仿这种行为的方法。
您可以看到它“正在运行”here
代码说明:
该代码只是一个 jQuery 插件,它查看 anchor 的 href 属性并将窗口位置设置为该值。我以典型的包装闭包模式进行编写,以本地化对 window 和 jQuery 的引用。我允许您在多个 anchor 上调用它,但我假设普通用户只需要运行一次。
关于javascript - 使用 Javascript/jQuery 出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619840/