简体   繁体   English

jQuery在我的网站上不起作用

[英]jQuery not Working on my site

I have this code: http://jsfiddle.net/AH4As/3/ 我有以下代码: http : //jsfiddle.net/AH4As/3/

It works in the fiddle, but on my site, it does not. 它在小提琴中有效,但在我的网站上则无效。 I'm getting this error in Web Inspector 我在Web检查器中遇到此错误

Tracking.js:4TypeError: 'undefined' is not an object (evaluating '$('a').attr('onClick').replace')

Does anybody know what wrong? 有人知道错在哪里吗?

Here's my source code: http://jsfiddle.net/AH4As/24/ 这是我的源代码: http : //jsfiddle.net/AH4As/24/

UPDATED: 更新:

$(document).ready(function(){
    $('a').attr('onClick', $('a').attr('onClick').replace("window.open('", "window.open('http://example.com/"));
});

​

http://jsfiddle.net/AH4As/10/ http://jsfiddle.net/AH4As/10​​/

And here it is with an alert to show you the code has been changed: http://jsfiddle.net/AH4As/11/ 并在其中显示警告,告诉您代码已更改: http : //jsfiddle.net/AH4As/11/

尝试:

$('a').removeAttr('onclick').click(function(){window.open('http://example.com');return false;});

What about: 关于什么:

$(document).ready(function(){
    $('a').removeAttr('onclick');
    $('a').click(function(){
        window.open('http://www.example.com', '','location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460');
    });
});

You won't be able to (reliably) replace on the onclick with jQuery 1.4: 您将无法(可靠地)用jQuery 1.4 replace onclick

'function' === typeof $('a').attr('onclick');

And Functions don't have replace methods: 而且Function没有replace方法:

'undefined' === typeof $('a').attr('onclick').replace;

You're getting a function because jQuery is returning the property value rather than the attribute value. 之所以得到一个函数,是因为jQuery返回的是属性值而不是属性值。

The confusion from this is why jQuery added the .prop() method in 1.6 -- so it's clear that you want the attribute value, not the property value: 对此的困惑是jQuery在1.6中添加.prop()方法的原因-很明显,您需要属性值,而不是属性值:

// with jQuery 1.6+
'string' === typeof $('a').attr('onclick');

Now, if you're not able to update jQuery currently, you can try inserting a toString before the replace : 现在,如果您当前无法更新jQuery,则可以尝试在replace之前插入toString

$('a').attr('onclick').toString().replace(...)

On a side-note: for better coupling of the original and adjusted value, you may look at using an alternate syntax for .attr() : 附带说明:为了更好地结合原始值和调整后的值,您可以考虑对.attr()使用替代语法:

$('a').attr('onclick', function (i, value) {
    return value.toString().replace('window.open(\'', 'window.open(\'http://example.com/'));
});

Other anchors on the page made the Query fail, in a nutshell this was my solution: 简而言之,这是我的解决方案:页面上的其他锚点使查询失败。

<script type="text/javascript">
$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://eastcoasttvs.com/"));
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM