开发者

Passing double quotes to Jscript

insertText is java script that accepts two string paramters

I need to pass two strings

first parameter:

<img src="

second

 ">

I just cant figure out how to pass double quote as parameter

This works

<a onClick="insertText('<em>', '</em>'); return false;">Italic</a>

This does not

<a onClick="insertText('<img src=/"', '/">'); return false;">Image</a>

Prints '); re开发者_开发百科turn false;">Image


You want to use \ rather than /


The escape character for JavaScript is \, not /. So try this:

<a onClick="insertText('<img src=\"', '\">'); return false;">Image</a>

Update:

The solution above doesn't work, because the double-quotes "belong" to the HTML and not to the JavaScript, so we can't escape them in the JavaScript code.

Use this instead:

<a onClick="insertText('<img src=\'', '\'>'); return false;">Image</a> // --> <img src='...'>

or

<a onClick='insertText("<img src=\"", "\">"); return false;'>Image</a> // --> <img src="...">

Since you are using jQuery, why don't you do it the jQuery way?

insertText = function(a, b) {
    // your insertText implementation...
};

$('a').click(function() {  // use the right selector, $('a') selects all anchor tags
    insertText('<img src="', '">');
});

With this solution you can avoid the problems with the quotes.

Here's a working example: http://jsfiddle.net/jcDMN/


The Golden Rule for that is reversing the quotation which means I use the single quotation ' inside the double quotation " and vice versa.

Also, you should use the backslash symbole to espape a special character like ' and ".

For example, the following commands should work as they apply the rules mentioned above...

<a onClick="insertText('<em>', '</em>'); return false;">Italic</a>

or

<a onClick='insertText("<em>", "</em>"); return false;'>Italic</a>

or

<a onClick="insertText('<img src=\"', '\">'); return false;">Image</a>

or

<a onClick='insertText("<img src=\'", "\'>"); return false;'>Image</a>

I hope this helps you ...


You need to escape it.

<a onClick="insertText('<img src=\"', '\">'); return false;">Image</a>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜