通过javascript/jquery使用onclick函数添加按钮
问题描述:
我正在努力寻找通过附加功能的javascript/jquery添加按钮的正确语法.
I'm struggling to find the correct syntax for adding a button via javascript/jquery that has a function attached.
现在我正在尝试:
list.append("<button onclick='getFeed('http://open.live.bbc.co.uk/weather/feeds/en/PA2/3dayforecast.rss, showFeedResults')'>ButtonTest</button>");
但是它返回:
<button onclick="getFeed(" http:="" open.live.bbc.co.uk="" weather="" feeds="" en="" pa2="" 3dayforecast.rss,="" showfeedresults')'="">ButtonTest</button>
基本上,我希望能够创建
Essentially, I want to be able to create
<button onclick="getFeed('http://open.live.bbc.co.uk/weather/feeds/en/B1/3dayforecast.rss', showFeedResults)" class="ui-btn-hidden">
在javascript中,但不知道如何..
In javascript, But can't figure out how..`
而且我似乎很难将精力集中在创建同时使用单和双qoutation标记的html元素上.
And I seem to be having trouble wrapping my head around creating html elements that use both single and double qoutation marks.
任何建议将不胜感激.
答
list.append("<button>ButtonTest</button>");
list.on("click", "button", function(){
getFeed('http://open.live.bbc.co.uk/weather/feeds/en/PA2/3dayforecast.rss, showFeedResults');
});
对于动态生成的元素,请使用上述的.on()
方法.
For dynamically generated elements use .on()
method like above.