2015-06-03 51 views
0

我有四個圖像按鈕與onclick事件附加到他們兩對兩個。我試圖用文本按鈕替換這些圖像按鈕,並將onclick功能重新附加到下一個文本按鈕。這些按鈕並不總是在頁面上,我只能使用jQuery 1.4,否則.prop會使這更容易。jQuery附加onclick函數元素

考慮這個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 (evaluating 'b.split')

input.one不存在。

我該如何做這項工作?

+0

你使用的是什麼版本的jquery? –

+0

@GuruprasadRao 1.4.4 – henryaaron

+0

您的_使用一些資源在線更新code_令人困惑!文件 –

回答

0

請嘗試以下代碼綁定onClick事件

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

不能這樣做。 Internet Explorer的Kryptonite – henryaaron

+0

我已檢查其在IE10中的工作方式http://jsfiddle.net/o1n4sx8e/ – Mitul

+0

@henryaaron - 您在示例中使用了'.attr()'。爲什麼這個答案是IE的關注呢? – wahwahwah

0

好吧,我建議你與你的第一選擇方法去:

入住這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). However, because the browsers treat the event attributes in a different way, they are returned with function onclick() { clickOne() } and you know about this. 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
  • wrap clickOne and clickTwo in head and remaining things on document.ready
+0

當按鈕不在頁面上時,代碼會中斷。 'TypeError:undefined不是一個對象(評估'$('input.one')。first()。get(0).attributes')' – henryaaron

+0

然後在獲取按鈕之前放置一個if條件來檢查它是否存在或不喜歡'if($('。input.one')。length> 0)'得到這個值。 –

+0

當我這樣做的時候得到這個錯誤。我不明白它'TypeError:undefined不是一個對象(評估'b.split')' – henryaaron