简体   繁体   English

jQuery将onclick函数附加到元素

[英]jQuery attach onclick function to element

I have four image buttons with onclick events attached to them two pairs of two. 我有四个带有onclick事件的图像按钮,它们分别是两两对。 I'm trying to replace those image buttons with text buttons and reattach the onclick function to the next text buttons. 我正在尝试将这些图像按钮替换为文本按钮,并将onclick函数重新连接到下一个文本按钮。 These buttons are not always on the page and I'm confined to using jQuery 1.4 otherwise .prop would make this a lot easier. 这些按钮并不总是在页面上,我只限于使用jQuery 1.4,否则.prop会使此操作变得容易.prop

Consider this HTML: 考虑以下HTML:

<input type="image" src="path/to/image.gif" class="one" onclick="function()" />
<input type="image" src="path/to/image.gif" class="one" onclick="function()" />
<input type="image" src="path/to/image2.gif" class="two" onclick="function()" />
<input type="image" src="path/to/image2.gif" class="two" onclick="function()" />

I wrote the following jQuery code to get the onclick function, the inputs and rebind the onclick function. 我编写了以下jQuery代码以获取onclick函数,输入并重新绑定onclick函数。

$('input.one').each(function(){
  var oneOnClick = $(this).attr('onclick');
  $(this).replaceWith('<input type="button" value="one" class="one" onclick="'+oneOnClick+'" />');
)};

And the same for .two . .two I found out that the way the value of the onclick attribute is returned in jQuery means this cannot be done. 我发现在jQuery中返回onclick属性的值的方式意味着此操作无法完成。

Using some resources online I updated my code to look like this: 使用在线一些资源,我更新了代码,如下所示:

var oneOnClick = $('input.one').first().attr('onclick');
var twoOnClick = $('input.two').first().attr('onclick');

$('input.one').each(function(){
  $(this).replaceWith('<input type="button" class="one" value="one"/>');
});
$('input.two').each(function(){
  $(this).replaceWith('<input type="button" class="two" value="two"/>');
});

$('input.one').live("click", oneOnClick);
$('input.two').live("click", twoOnClick);

But I'm getting an error 但是我出错了

TypeError: undefined is not an object (evaluating 'b.split') TypeError:undefined不是对象(评估'b.split')

when input.one doesn't exist. input.one不存在时。

How can I make this work? 我该如何进行这项工作?

Please try the following code to bind onClick event 请尝试以下代码来绑定onClick事件

$(document).ready(function(){
    $('input.one').each(function(){
       $(this).attr('type',"button");
    });
});

Well I suggest you to go with your first opted method: 好吧,我建议您采用第一个选择的方法:

Check this DEMO 检查这个演示

function clickOne()
{
    alert('hi');
}
function clickTwo()
{
    alert('helleo');
}

$(document).ready(function(){
    var oneOnClick = $('input.one').first().get(0).attributes.onclick.nodeValue;
    var twoOnClick = $('input.two').first().get(0).attributes.onclick.nodeValue;
    $('input.one').each(function(){
        $(this).replaceWith('<input type="button" value="one" class="one" onclick="'+oneOnClick+'" />');
    });

    $('input.two').each(function(){
        $(this).replaceWith('<input type="button" value="two" class="two" onclick="'+twoOnClick+'" />');
    });
    $('input.one').live("click", oneOnClick);
    $('input.two').live("click", twoOnClick);
});

POINT TO NOTE: 注意事项:

  • $('input.one').attr('onclick') or $('input.two').attr('onclick') holds the value of the onclick attribute (which is a string). $('input.one').attr('onclick')$('input.two').attr('onclick')保存onclick属性的值(它是一个字符串)。 However, because the browsers treat the event attributes in a different way, they are returned with function onclick() { clickOne() } and you know about this. 但是,由于浏览器以不同的方式处理事件属性,因此它们通过function onclick() { clickOne() }返回,您对此function onclick() { clickOne() }了解。 Thus, if you want to directly call the function, you'll have to either strip the function() {} or call it immediately or get it using get(0).attributes.onclick.nodeValue 因此,如果要直接调用该函数,则必须剥离function() {}或立即调用它,或者使用get(0).attributes.onclick.nodeValueget(0).attributes.onclick.nodeValue它。
  • wrap clickOne and clickTwo in head and remaining things on document.ready clickOneclickTwo包装在head并将其余内容包装在document.ready

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

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