javascript - How to dynamically set the attribute class="" using js?
PHP中文网
PHP中文网 2017-05-19 10:15:43
0
5
532

I now want to set an element with <p class=""></p>, but
var p=$('<p></p>').attr({ 'class':""});
The <p class></p> attribute set in this way is empty, and escaping has no effect. Maybe there is something wrong with my writing.
May I ask for a solution? ~

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
習慣沉默

You can use
.attr('class', null)
or
.removeAttr('class')

刘奇

$('body'). append('<p></p>').attr("class", "classname")
刘奇
var pHtml = "<p>This is a p element</p>";
$("body").html(pHtml );
$("p").addClass("\'\'");

You can try this to see if it is the effect you want.

phpcn_u1582

For elements whose attributes are equal to empty strings, the browser will automatically retain only the attributes. What are you planning to do by setting this?

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

You get <p class>haha</p> which is equivalent to <p class=''>haha</p>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!