我正在努力寻找通过 javascript/jquery 添加带有附加功能的按钮的正确语法。
现在我正在尝试:
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>
本质上,我希望能够创建
<button onclick="getFeed('http://open.live.bbc.co.uk/weather/feeds/en/B1/3dayforecast.rss', showFeedResults)" class="ui-btn-hidden">
在javascript中,但不知道如何......`
我似乎在创建同时使用单引号和双引号的 html 元素时遇到了麻烦。
任何建议将不胜感激。
附加分配有单击事件侦听器的按钮
使用纯 JavaScript
const NewEL = (tag, prop) => Object.assign(document.createElement(tag), prop);
// Use like:
const EL_btn = NewEL("button", {
textContent: "Test button",
onclick() { console.log(this); /* getFeed etc... */ },
});
document.body.append(EL_btn);
或者使用jQuery library
http://api.jquery.com/on/ http://api.jquery.com/on/
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()
方法如上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)