5

我的网站上有以下图形按钮:

<a href="#" class="addto_cart_btn">
  <span id="btn_text">Click here to add to cart now</span>             
</a>

我想在单击时运行特定的 javascript 脚本(当前值 #)以运行一些 javascript - javascript 代码本质上会生成一个弹出 iFrame,其中包含其他内容。

实现这一目标的最佳方法是什么?

4

4 回答 4

5

尝试这个

<script type="text/javascript">
   window.onload = function() {
      document.getElementById("btn_text").onclick = function() {
         // Do your stuff here
      };
   };
</script>

或者如果你可以使用 JQuery

<script type="text/javascript">
   $(document).ready(function() {
      $("#btn_text").click(function(){
         // Do your stuff here
      });
   });
</script>
于 2013-10-01T10:48:22.620 回答
4

一种方法是添加 onclick 属性

   <a href="#" class="addto_cart_btn" onclick="someFunction()">
      <span id="btn_text">Click here to add to cart now</span>             
    </a>

然后是javascript:

function someFunction(){
   //do stuff
}
于 2013-10-01T10:49:14.547 回答
0

您应该使用 OnClick 属性

<a href="#" class="addto_cart_btn" onclick="yourFunction();">
  <span id="btn_text">Click here to add to cart now</span>             
</a>
于 2013-10-01T10:49:01.710 回答
0

对于普通的 javascript

window.onload = function(){
   document.getElementById('btn_text').onclick = function(){
      // do stuff;
   }
}

对于 jQuery

jQuery(function($){
   $('#btn_text').on('click', function(){
      // do stuff
   })
})
于 2013-10-01T10:52:13.813 回答