【问题标题】:jQuery attach onclick function to elementjQuery将onclick函数附加到元素
【发布时间】:2015-06-03 04:57:27
【问题描述】:

我有四个带有 onclick 事件的图像按钮,两对两对。我正在尝试用文本按钮替换这些图像按钮,并将 onclick 功能重新附加到下一个文本按钮。这些按钮并不总是在页面上,我仅限于使用 jQuery 1.4,否则.prop 会让这变得更容易。

考虑一下这个 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()" />

我编写了以下 jQuery 代码来获取 onclick 函数、输入和重新绑定 onclick 函数。

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

.two 也是如此。我发现在jQuery中返回onclick属性的值的方式意味着无法做到这一点。

使用一些在线资源,我将代码更新为如下所示:

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);

但是我遇到了一个错误

TypeError: undefined is not an object (evalating 'b.split')

input.one 不存在时。

我怎样才能做到这一点?

【问题讨论】:

  • 您使用的是什么版本的jquery
  • @GuruprasadRao 1.4.4
  • 您的使用一些资源在线更新代码 令人困惑! next_page_img 存在于文档的什么位置
  • 固定@GuruprasadRao
  • var twoOnClick = $('input.next_page_img').first().attr('onclick'); 怎么样。如果我没记错的话应该是input.two!!

标签: javascript jquery html onclick


【解决方案1】:

请尝试以下代码绑定onClick事件

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

【讨论】:

  • 不能那样做。 Internet Explorer 的氪石
  • @henryaaron - 您在示例中使用.attr()。那么为什么这个答案会引起 IE 的关注呢?
  • @wahwahwah 在许多版本的 Internet Explorer 中都不允许更改输入的类型
【解决方案2】:

好吧,我建议您使用第一个选择的方法:

检查这个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);
});

注意事项:

  • $('input.one').attr('onclick')$('input.two').attr('onclick') 保存 onclick 属性的值(这是一个字符串)。但是,由于浏览器以不同的方式处理事件属性,因此它们是 返回 function onclick() { clickOne() } 你知道 对这个。因此,如果您想直接调用该函数,您将 必须要么剥离 function() {} 或立即致电或使用 get(0).attributes.onclick.nodeValue
  • clickOneclickTwo 包裹在 head 中,剩下的东西放在 document.ready

【讨论】:

  • 当按钮不在页面上时,您的代码会中断。 TypeError: undefined is not an object (evaluating '$('input.one').first().get(0).attributes')
  • 然后在获取按钮之前放置一个 if 条件来检查它是否存在,例如 if($('.input.one').length&gt;0) 获取值。
  • 当我这样做时得到这个错误。完全看不懂TypeError: undefined is not an object (evaluating 'b.split')
  • 我怀疑它可能来自其他来源,而不是来自上述代码。只需检查您在哪里收到 error 消息!
  • 这是什么意思? var inputOneElement = $('input[type="image"].one'); var inputTwoElement = $('input[type="image"].two');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2018-09-12
  • 2014-02-14
  • 1970-01-01
相关资源
最近更新 更多