简体   繁体   中英

Escaping single quote

I have a div like this:

<div class='myclass'></div>

Sometimes i need to make these div dynamically and add another class to it. I do it like this

if(condition1)
  appendClass = 'xyz'
if(condition2)
  appendClass = 'abc'
if(condition3)
  appendClass = 'usb'

$('main').append("<div class = myclass "+ appendClass +"/>")

but when i check this div with firebug i get this

 <div class="myclass" xyz=""/>

which should have been

 <div class="myclass xyz"/>

I tries using append.replace(/"/g, '') but same result. Please help!!

$('main').append("<div class =' myclass "+ appendClass +"' />")

但我个人的喜好是:

$('main').append('<div class="myclass ' + appendClass + '"/>');

你也可以做这样的事情

$("<div>").addClass("myclass " + appendClass).appendTo($(".main"))

您可以混合使用单引号和双引号来引用字符串中的内容,例如...

$('.main').append("<div class='myclass " + appendClass + "' />")

尝试这个

$( ".myclass").addClass(appendClass);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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