jquery通过attr获取属性值为什么都是undefind?


图片描述 图片描述
这里的jQuery和$是等效的,
为什么通过attr改变标签的display不起作用,
jQuery(this).next('ul').attr("display")得到的是undefind。


 jQuery(document).ready(function($){
    jQuery("#cataloguelist").children("ul li:first").children("ul").attr('dispaly','block');


    jQuery(".grayp").on('click',function(){
      var tr=jQuery(this).next('ul').attr("display");
      if(tr=="none"){
        jQuery(this).next('ul').attr("display","block")
      }
      else{
        jQuery(this).next('ul').attr("display","none");
      }
    });
  });

jquery web前端开发 JavaScript

白鷺茉百合 10 years, 12 months ago

用attr()应该是取到style属性

45363w answered 10 years, 12 months ago

display 不是标签的style 属性么?

倒挂的粮仓 answered 10 years, 12 months ago

看你的意思是想取CSS属性,应该用 css() 而不是 attr

feiting answered 10 years, 12 months ago

你是要通过JQuery来控制元素的显示和隐藏吗? attr 是获取DOM的attribute,而 display 是属于样式中规则名,不属于attribute,所以会返回 undefined

可以利用 css() 方法实现,给一个参考的Demo: Demo

HTML 中,还需要区分 attribute property : HTML中的attribute和property

我看了电视 answered 10 years, 12 months ago

Your Answer