Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Wie setze ich das Attribut class="" dynamisch mit js?

Ich möchte jetzt ein Element mit <p class=""></p> festlegen, aber
var p=$('<p></p>').attr({'class' : ""});
Das so festgelegte Attribut <p class></p> hat keine Auswirkung. Vielleicht stimmt etwas mit meinem Schreiben nicht

PHP中文网PHP中文网2710 Tage vor537

Antworte allen(5)Ich werde antworten

  • 習慣沉默

    習慣沉默2017-05-19 10:17:43

    可以用
    .attr('class', null)
    或者
    .removeAttr('class')

    Antwort
    0
  • 怪我咯

    怪我咯2017-05-19 10:17:43

    
    $('body'). append('<p></p>').attr("class", "classname")

    Antwort
    0
  • 怪我咯

    怪我咯2017-05-19 10:17:43

    var pHtml = "<p>This is a p element</p>";
    $("body").html(pHtml );
    $("p").addClass("\'\'");

    你可以试下这样的,是不是你想要的效果。

    Antwort
    0
  • phpcn_u1582

    phpcn_u15822017-05-19 10:17:43

    属性等于空字符串的元素 浏览器会自动只保留属性的 你设置这个是准备做什么

    Antwort
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:17:43

      var p1= $("<p class>haha</p>")
      $("body").append(p)
      $(".addItem").click(function(){
        alert($('p').hasClass(""))  //true
      })

    你得到的<p class>haha</p> 相当于 <p class=''>haha</p>

    Antwort
    0
  • StornierenAntwort