我有四個圖像按鈕與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
不存在。
我該如何做這項工作?
你使用的是什麼版本的jquery? –
@GuruprasadRao 1.4.4 – henryaaron
您的_使用一些資源在線更新code_令人困惑!文件 –