Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用jquery在dom就绪上添加属性_Javascript_Jquery_Jquery 1.4 - Fatal编程技术网

Javascript 无法使用jquery在dom就绪上添加属性

Javascript 无法使用jquery在dom就绪上添加属性,javascript,jquery,jquery-1.4,Javascript,Jquery,Jquery 1.4,我有一本书 我们正在使用jQuery1.4.4,到目前为止无法更新到更新的版本,在那里我可以让代码正常工作 因此,我需要在domready或pageload上,将attr添加到众多中,您可以使用过程轻松完成,如下所示: $('a').attr('href', function(index, oldAttr) { if (oldAttr.indexOf('test') >= 0) { $(this) .removeAttr('onclick')

我有一本书

我们正在使用jQuery1.4.4,到目前为止无法更新到更新的版本,在那里我可以让代码正常工作

因此,我需要在domready或pageload上,将attr添加到众多
中,您可以使用过程轻松完成,如下所示:

$('a').attr('href', function(index, oldAttr) {
    if (oldAttr.indexOf('test') >= 0) {
        $(this)
            .removeAttr('onclick')
            .get(0)
            .setAttribute("onclick", "test");
    }
});​

但如果要使用
.each()
,请尝试以下操作:

$('a').each(function(index, element) {
    var el = $(element),
        hasHref = !el.attr('href'); // return true, if no href
    if( !hasHref ) {
        var href = el.attr('href'); 
        if( href.match('test').length || href.match('test-page').length ) {
         el
           .removeAttr("onclick")
           .get(0)
           .setAttribute("onclick", "test");
        }
    }
});​

如果有jQuery,为什么要使用事件属性?@Bergi这是一个很好的观点!它的功能与我原来的相同,它删除了,但没有添加回去。感谢您的帮助,还希望用户检查:@Bergi评论建议的替代答案