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

时间:2015-12-22 07:07:55

标签: javascript object

$(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);
});

使用这个看似基本的对象设置获取错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您应该在声明var后删除unfollow_tag,然后重新声明它,而不是尝试访问媒体资源。

$(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);
});