简体   繁体   English

Javascript / Jquery引号和双引号转义

[英]Javascript/Jquery quote and double quotes escaping

A simple jquery line of code: 简单的jquery代码行:

if ($j('#page2').length) {
    $j('#myPlaceHolder2').html('<button class=\"button3\" type=\"button\">Page II</button>');
}

This works perfectly fine. 这工作得很好。 When I later create a span with id myPlaceHolder2, it displays the button. 当我以后创建ID为myPlaceHolder2的跨度时,它将显示按钮。

Now I need to add an onclick event 现在我需要添加一个onclick事件

 onclick=\"javascript:  $j('#'+openc).fadeOut('normal', function(){$j('#page2').fadeIn('slow')});openc='page2';\"

To the above .html, as follows 到上面的.html,如下

   $j('#myPlaceHolder2').html('<button class=\"button3\" type=\"button\" onclick=\"javascript:  $j('#'+openc).fadeOut('normal', function(){$j('#page2').fadeIn('slow')});openc='page2';\">Page II</button>');

And all hell breaks loose. 和所有地狱打破。 I tried escaping each ' with \\ but it breaks my site entirely for some reason. 我尝试使用\\对每个'进行转义,但是由于某种原因,它完全破坏了我的网站。 I'm not very familiar with javascript or jquery, only learning as I go, so any indication as to how to get this done would be helfpul, thank you! 我对javascript或jquery不太熟悉,只能自己学,所以有关如何完成此操作的任何指示都将成为helfpul,谢谢!

Add it with jQuery: 用jQuery添加它:

$j('#myPlaceHolder2')
    .html('<button class=\"button3\" type=\"button\">Page II</button>')
    .find('.button3')
    .click(function () {
        $j('#'+openc).fadeOut('normal', function(){$j('#page2').fadeIn('slow')});openc='page2';
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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