简体   繁体   English

使用对象文字的JQuery无法使用$(this)始终在onclick上取消定义

[英]JQuery using object literal can't using $(this) always undefine on onclick

my problem after write as object literal as Jquery guide i can't using $(this) to access self on onlick="". 写作对象文字作为Jquery指南后我的问题我不能使用$(this)来访问on on on =“”。 please help correct my mistake. 请帮助纠正我的错误。

my html 我的HTML

<a 
 data-id="<?=$product_id?>" 
 class="compare product-<?=$product_id?>" 
 onclick="(function(){compareInit.comGet();})()"
></a>

my js 我的js

   var compareInit = {

     /*  Store Item Compare  */
      comGet: function() {

          var e = $(this);
          var item_id = e.data('id');
          var item_image = e.find(".compare-hidden-image").val();
          var item_name = e.find(".compare-hidden-name").val();
          var count_item = $(".compare-item").length;
          var item_dialog = $(".compare-tray-dialog");
          var compare_button = $(".compare-tray-item");
          item_dialog.show();

          if (count_item > 1) {
          } else {
            $(".product-"+ item_id).css("color", "red").attr('onclick','');
          }
          if (count_item === 0) {
            compare_button.removeClass('activate').addClass('deactivate');
          } else {
            compare_button.removeClass('deactivate').addClass('activate');
          }
          $('.compare-remove').on("click", function() {
            var rem_id = $(this).data('id');
            $("." + rem_id).remove();
            $(".product-" + rem_id).css("color", "#fff").attr('onclick','(function(){compareInit.comGet();})()');
            compare_button.removeClass('activate').addClass('deactivate');
          });
      }
    };

Thank in advance. 预先感谢。

You can pass the this identifier from the onclick event and then access it under a name other than this such as elem as a parameter of your function. 你可以通过this从onclick事件标识符,然后访问它在比其他的名称thiselem作为函数的参数。

 var compareInit = { /* Store Item Compare */ comGet: function(elem) { console.log("working"); var e = $(elem); var item_id = e.data('id'); var item_image = e.find(".compare-hidden-image").val(); var item_name = e.find(".compare-hidden-name").val(); var count_item = $(".compare-item").length; var item_dialog = $(".compare-tray-dialog"); var compare_button = $(".compare-tray-item"); item_dialog.show(); if (count_item > 1) {} else { $(".product-" + item_id).css("color", "red").attr('onclick', ''); } if (count_item === 0) { compare_button.removeClass('activate').addClass('deactivate'); } else { compare_button.removeClass('deactivate').addClass('activate'); } $('.compare-remove').on("click", function() { var rem_id = $(this).data('id'); $("." + rem_id).remove(); $(".product-" + rem_id).css("color", "#fff").attr('onclick', '(function(){compareInit.comGet();})()'); compare_button.removeClass('activate').addClass('deactivate'); }); } }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a data-id="3636" class="compare product-3636" onclick="compareInit.comGet(this)">Testing</a> 

Try this: apply the this inside the onclick function .If you apply this in object function its get the data from that object only 试试这个:应用this的onclick函数内部。如果你申请在此目标函数的从那个对象中的数据只

 var compareInit ={ comGet : function(that){ console.log(that.innerHTML) } } 
 <a onclick="compareInit.comGet(this)">hello</a> 

Alternate: 备用:

If get the this from whole object try with return like below .its like a jquery object $(element).html() 如果从整个对象中获取this函数,请尝试使用如下所示返回。例如jquery对象$(element).html()

 var compareInit = function(that){ return { comGet : function(){ console.log(that.innerHTML) } } } 
 <a onclick="compareInit(this).comGet()">hello</a> 

You need to pass-on the your required DOM element's this reference as follows: 你需要传递对您所需的DOM元素的this参考如下:

onclick="(function(){compareInit.comGet();})()" ; onclick="(function(){compareInit.comGet();})()" ; here you are invoking an anonymous function without passing anything to it. 在这里你调用一个匿名函数而不传递任何东西。 So there inside it this reference means that anonymous function itself. 所以在它里面this引用意味着匿名函数本身。 To achieve your goal you need to pass DOM reference as follows: 要实现您的目标,您需要传递DOM引用,如下所示:

 var compareInit = { /* Store Item Compare */ comGet: function(thisRef) { alert($(thisRef).text()); } }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="mydiv" onclick="(function(thisRef){compareInit.comGet(thisRef);})(this)">Click Me!</div> 

我认为你需要将JQuery文件与这样的网页链接起来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM