通过javascript/jquery使用onclick函数添加按钮 [英] Adding a button, with onclick function, via javascript/jquery

查看:114
本文介绍了通过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.

任何建议将不胜感激.

推荐答案

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()方法.

For dynamically generated elements use .on() method like above.

这篇关于通过javascript/jquery使用onclick函数添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆