4

I am trying to add this HTML/JavaScript code into a jQuery variable. I've managed to insert double quotes by writing is with a backshlash \", however the same tactic didn't work for the single quotes:

ajaxData += '<div class=\"menu-item-' + $(this).attr('div') + 'onclick=\"alert('Jquery Function');\"></div>';

Specifically, this part onclick=\"alert('Jquery Function');

Anyone know how I can go around this?

4

4 回答 4

7

看到这个,很漂亮:

ajaxData += '<div class="menu-item-' + $(this).attr('div') + ' onclick="alert(\'Jquery Function\');"></div>';
于 2012-12-11T17:54:47.800 回答
2

这是你想做的吗?

$var = "ajaxData += '<div class=\"menu-item-' + \$(this).attr('div') + '" onclick=\"alert(\'Jquery Function\');\"></div>';"
于 2012-12-11T18:01:30.147 回答
2

肮脏的逃生pheeww ...试试这个

ajaxData += '<div class="menu-item-' + $(this).attr('div') + 'onclick="alert(\'Jquery Function\');"></div>';
于 2012-12-11T17:54:24.880 回答
2
ajaxData += '<div class="menu-item-' + $(this).attr('div') + 'onclick="alert('Jquery Function');"></div>';

为单引号添加转义 \。如果您的字符串在单引号内,那么您可以使用双引号而无需转义,但如果在单引号内使用单引号,则必须插入转义字符

于 2012-12-11T17:55:44.380 回答