单击span元素时如何执行函数?

时间:2014-09-25 07:30:22

标签: javascript jquery

尝试执行未从span元素触发的函数onclick_noservice_button()。如何解决?

<div class="rightclick_gui" id="rightclick_gui_noservice">

  <div class="rightclick_gui_body"></div>
</div>

function onclick_noservice_button() {
  alert("OK");
}


function rightclick_buttons_noservice() {
  $('#rightclick_gui_noservice').show();

  var html = '';
  $.ajax({
    type    : "GET",//POST
    dataType: 'json',
    url     : main_url + '/ajax',
    data    : 'code=noservice',
    async   : true,
    beforeSend: function() { 
    },
    complete: function() {                   
    },
    success : function(msg) {
      goal = msg.result;
      for(var key in goal) {
        var obj = goal[key];
        if(obj.hasOwnProperty("username")) {

          html +='<div class="rightclick_gui_video">';

          html +="<div style='background-color:orange;' class='available' id='" + 
                      obj['username'] + "'><span onclick='onclick_noservice_button();'>" + obj['username'] + "</span></div>"; 

          html +='</div>';

        }       
      }

      $('#rightclick_gui_noservice .rightclick_gui_body').html(html);

    }
  }); 

}

2 个答案:

答案 0 :(得分:0)

使用下面的代码。

  • HTML

<span id='demoSpan'>Content....... Click here </span>

  • 的Javascript

$(document).ready({ $("#demoSpan").click(function(){ alert('span clicked...!!); }); });

答案 1 :(得分:-1)

你正在调用 righclick_gui_close()点击范围,范围内不存在。尝试调用 onclick_noservice_button(),就像下面的代码所示。

<div class="rightclick_gui" id="rightclick_gui_noservice">
  <div class="rightclick_gui_close">No Service - <span onclick="onclick_noservice_button();">X</span>
</div>
<div class="rightclick_gui_body"></div>
</div>