简体   繁体   English

使用点表示法的 JavaScript 对象错误

[英]JavaScript Objects Error using dot notation

$(document.body).on("click",'.sub-unfollow', function(){
  var unfollow_tag = {element:"",un:"",type:"",text:""};
  var unfollow_tag.element = $(this).parents("li");
  var unfollow_tag.un = $(this).parents("li").attr("data-un");
  var unfollow_tag.type = $(this).parents("li").attr("data-type");
  var unfollow_tag.text = $(this).parents("li").text();
  alert(unfollow_tag.text);
});

Getting an error with this seemingly basic object setup.这个看似基本的对象设置出错。 Any ideas?有任何想法吗?

You should remove var after once declaring unfollow_tag , it redeclares it instead of trying to access a property.您应该在声明unfollow_tag后删除var ,它重新声明它而不是尝试访问属性。

$(document).on("click",'.sub-unfollow', function(){
  var unfollow_tag = {};
  unfollow_tag.element = $(this).parents("li");
  unfollow_tag.un = $(this).parents("li").attr("data-un");
  unfollow_tag.type = $(this).parents("li").attr("data-type");
  unfollow_tag.text = $(this).parents("li").text();
  alert(unfollow_tag.text);
});

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

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