2013-05-02 81 views
0

HTML代碼兩個功能和警報不工作?

<body> 
<h1 align="center">Are you ready?</h1> 
<h2 align="center">Just answer Yes or No!</h2> 
<div class="wrapper"> 
    <button id="ohyes" class="buttonYes"> Yes </button> 
    <button id="ohno" class="buttonNo"> No </button> 
</div> 
</body> 

jQuery代碼

<script> 
$(function() { 
    $("#ohno").on({ 
     mouseover: function() { 
      $(this).css({ 
       left: (Math.random() * 800) + "px", 
       right: (Math.random() * 800) + "px", 
       top: (Math.random() * 400) + "px", 
      }); 
     } 

    }); 

    $("#ohyes").click(function() { 
     alert("yes"); //use .val() if you're getting the value 
    }); 
}); 
</script> 

我試圖調用jQuery函數首先是工作的罰款按鈕移動鼠標懸停但是當我點擊ID爲按鈕ohyes不顯示任何警報框? 有什麼建議嗎?

+5

您可能正在使用Internet Explorer,並且由於traili語法錯誤ng','在你的物體中。 – 2013-05-02 11:18:28

+0

如果它是動態創建的元素,您可能想要使用.on委託,就像您用於#ohno按鈕 – 2013-05-02 11:18:39

+0

@ÁlvaroG.Vicario - 敏銳的目光。 '+ 1' – ahren 2013-05-02 11:18:50

回答

0

試試這個:點擊事件後會在第二個按鈕ohyes上發出警報。

$(document).ready(function(){ 
    $("#ohno").mouseover(function(){ 
     $(this).css({ 
      left:(Math.random()*800)+"px", 
      right:(Math.random()*800)+"px", 
      top:(Math.random()*400)+"px", 
     }); 
    }); 

    $("#ohyes").click(function(){ 
     alert("yes"); //use .val() if you're getting the value 
    }); 
}); 
+0

不工作的人。 – 2013-05-02 11:27:01

+0

你能告訴我你的html代碼嗎?因爲我已經在我的本地和它的工作正常,所以告訴我你的HTML代碼。 – 2013-05-02 11:28:18

+0

發表在問題。 – 2013-05-02 11:29:10

0

嘗試包裝你的ohyes功能的文檔中的準備塊 或者使用該 jQuery(function($){...});
或本
$(document).ready(function(){...});

與ohyes功能
代替省略號正如你可以看到你的..代碼正常工作http://jsfiddle.net/willypt/xsyxV/2/