Tinymce - insert html code

99封情书 提交于 2020-01-01 06:13:27

问题


I would appreciate if there is someone who can help me solve this problem, I've been trying to resolve it for few days, but with no success. I made custom button that inserts image into code, and here is textual version:

<a href="javascript:;" onmousedown="tinyMCE.execCommand('mceInsertContent',false,'<br><img alt=$img_title src=$link/img/sadrzaj/$file\>');">Insert Image</a>

Problem is that I'm not allowed to use quotes so I have to use alt=Some_value and quotes are added automaticly. Problem comes when i enter value with space: alt=Some value and then only "Some" is entered while everything after whitespace is ignored. For src attribute it's ok because it cant contain whitespaces, but for image description it would be very useful. I can format string and put _ instead of whitespace but that's not solution.

Any help, please?


回答1:


Add a backslash (\) before the quotes you want to include.

<a href="javascript:;" 
  onmousedown="tinyMCE.execCommand('mceInsertContent',false,'<br>
  <img alt=\'$img_title\' src=\'$link/img/sadrzaj/$file\' />');">Insert Image</a>

This will allow you to include the script and the quotes without it breaking on you.

The backslash just says to the code, "Don't stop here, this quote does not signal the end of the code line. Just include this in the display output. Thanks."

Note that you will have to use single quotes (') around the IMG attributes.

(Another thing: In XHTML, when self-closing a tag like IMG, you use the slash (/) and not the backslash.)




回答2:


It might be better to make your code more readable (if you ever need to go back and change things), you could just make the onmousedown call a function:

<a href="javascript:;" onmousedown="addImage()">Insert Image</a>

<script type="text/javascript">
function addImage(){
 tinyMCE.execCommand('mceInsertContent',false,'<br>
  <img alt="$img_title" src="$link/img/sadrzaj/$file" />');
}
</script>


来源:https://stackoverflow.com/questions/1514005/tinymce-insert-html-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!