How can I pass """ to a Javascript function-call made inside an HTML-tag?
I have a Javascript function (named insert_formatText) which inserts text into a specified textarea. This function takes 4 parameters: - opentag (the open tag) - closetag (the close tag) - formid (id-attribute value of the form) - elementid (id-attribute value of the textarea)
My function works very well except for when you set opentag as """ and closetag as """. In that case, it simply breaks.
<a href="javascript:;" onclick="insert_formatText('"""', '"""', 'foo', 'bar');">INSERT TRIPLE-QUOTES</a>
Extra Detail
I actually use PHP to loop through an array containing the opentag and closetag values as a key => value pair. In the loop, I pass those values to a function:
function editorButton($pre, $suf, $fid, $eid, $label){
$str = <<<EOT
<a href="javascript:;" onclick="insert_formatText('$pre', '$suf', '$fid', '$eid');">$label</a>
EOT;
return $str;
}
So my question: is there 开发者_Python百科anyway to get this to work for double-quotes without much changing? Or will I have to find a different method to generate these buttons?
You can escape the quotes using a backslash in this manner:
<a href="javascript:;" onclick="insert_formatText('\"\"\"', '\"\"\"', 'foo', 'bar');">INSERT TRIPLE-QUOTES</a>
you have to add slashes:
function editorButton($pre, $suf, $fid, $eid, $label){
$pre = addslashes($pre);
$suf = addslashes($suf);
$str = <<<EOT
<a href="javascript:;" onclick="insert_formatText('$pre', '$suf', '$fid', '$eid');">$label</a>
EOT;
return $str;
}
then it will yield you:
<a href="javascript:;" onclick="insert_formatText('\"\"\"', '\"\"\"', 'foo', 'bar');">INSERT TRIPLE-QUOTES</a>
Well, you are inside a tag and you've opened a string as ". So you need escapes
<a href="javascript:;"
onclick="insert_formatText('\"\"\"', '\"\"\"', 'foo', 'bar');"
>INSERT TRIPLE- QUOTES</a>
Or the other way around, you can try this
<a href="" onclick='insert_formatText("\"\"\"", "\"\"\"" "foo", "bar");'>INSERT TRIPLE- QUOTES</a>
You should encode your quotes as entities in the html. i.e.
<a href="javascript:;" onclick="insert_formatText('"""', '"""', 'foo', 'bar');">INSERT TRIPLE-QUOTES</a>
You can use the PHP function htmlentities for this.
精彩评论