使用jQuery动态创建按钮 [英] Creating buttons dynamically using jQuery
本文介绍了使用jQuery动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery动态创建按钮:
I am creating buttons dynamically using jQuery:
function createRefreshButton() {
var $btn = $('<button/>', {
text: 'Refresh Data',
id: 'btn_refresh'
});
$($btn).on('click', function () {
ClickRefresh()
});
return $btn;
}
我想知道是否可以将此代码更改为:
I was wondering if it is possible to change this code to something like this:
function createRefreshButton() {
var $btn = $('<button/>', {
text: 'Refresh Data',
id: 'btn_refresh',
onclick: 'ClickRefresh()'
});
return $btn;
}
但是当我点击具有该语法的按钮时没有任何反应。这是由于我建议的代码中的错误还是根本不可能?提前谢谢。
However nothing happens when I click the button with that syntax. Is that due to errors in my suggested code or is it simply impossible? Thank you in advance.
推荐答案
您可以设置点击
属性对象,而不是 onclick
,并为其提供 ClickRefresh
函数的引用。试试这个:
You can set the click
property of the object, not onclick
, and provide it the reference to the ClickRefresh
function. Try this:
function createRefreshButton() {
return $('<button/>', {
text: 'Refresh Data',
id: 'btn_refresh',
click: ClickRefresh
});
}
这篇关于使用jQuery动态创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文