0

我已经问过类似的问题标签,但我发现该解决方案无法使用标签。所以,如果我使用的是引用 jQuery Mobile 的脚本,我有以下行:

<button id="buttonAnswer1" data-inline="true">Click me</button>

当单击此按钮并调用 hello() 函数时,我将如何添加一个侦听器?IE

<button id="buttonAnswer1" data-inline="true">Click me</button>

<script>
function hello(){
  console.log("hello world");
}
</script>
4

2 回答 2

1

Are you wanting to select all <button> with data-line set to true?

if so, try this: http://jsfiddle.net/M9pwp/2/

html:

<button id="buttonAnswer1" data-inline="true">Click me</button>

script:

$("button").click(function() {
   if ($(this).attr('data-inline') == 'true')
      hello();
});

function hello()
{
     alert('hi');   
}

if you only want a listener for this particular button, then you can change the script to:

$("#buttonAnswer1").click(function() {
    hello();
});

function hello()
{
    alert('hi');   
}
于 2012-12-10T05:26:31.317 回答
1

您能否参考以下代码。

$("button#buttonAnswer1").on("click",hello);

请在 jsfiddle 中尝试上面的代码,让我知道您的疑虑。

http://jsfiddle.net/

于 2012-12-10T05:16:17.760 回答