我需要知道我的jQ函数是从哪里调用的……
在head中:
function call_pl2(){
    $(this).text('some text');
}正文部分:
<p> <script> call_pl2();  </script> </p>
<!--              OR                 -->
<div> <script> call_pl2();  </script> </div>发布于 2016-02-22 01:54:14
您可以显式地将包含元素作为参数传递给call_pl2(),而不是尝试确定哪个元素包含script标记(以及通过扩展,对call_pl2()的特定调用):
$(function() {
  var call_p12 = function(element) {
    if ($(element).is('p')) {
      $(element).text('here is some text added to a paragraph');
    }
    if ($(element).is('div')) {
      $(element).text('here is some text added to a div');
    }
  }
  $('div, p').each(function() {
    call_p12($(this));
  });
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p></p>
<div></div>
将call_p12()函数修改为在jQuery is()中交换更具体的选择器相对容易。例如,使用is('.someclass')检查类值而不是标记名。
https://stackoverflow.com/questions/35536600
复制相似问题