简体   繁体   English

将javascript插入ckeditor主体

[英]Insert javascript in to body of ckeditor

I use CKEDITOR 4.6 我使用CKEDITOR 4.6

I set this code(Html,JavaScript) to source of CKEDITOR 我将此代码(HTML,JavaScript)设置为CKEDITOR的源代码

<html>
<head>
    <title></title>
</head>

<body dir="rtl">
    <p>Click the button to display an alert box:</p>
    <p><button onclick="myFunction()">Try it</button>
        <script>
            function myFunction() {
                alert("I am an alert box!");
            }
        </script>
    </p>
</body>

</html>

But when i click on button this java script code does not worked? 但是,当我单击按钮时,此Java脚本代码不起作用? 在此处输入图片说明 在此处输入图片说明

How can run javascript in CKEDITOR? 如何在CKEDITOR中运行JavaScript?

Also i set this config but still not worked 我也设置了这个配置但是还是没有用

CKEDITOR.editorConfig = function( config ) {

     config.allowedContent = {
        script: true,
        allowedContent :true,
        $1: {
            // This will set the default set of elements
            elements: CKEDITOR.dtd,
            attributes: true,
            styles: true,
            classes: true
        }
    };
};

there are a few things wrong with your code: 您的代码有些错误:
1: you shouldn't put a <script> tag in a <p> element. 1:您不应在<p>元素中放置<script>标记。
2: most of the time, you need to execute your JS after the page has been loaded fully, so putting it in the header won't fix it, put it after all your content has been loaded. 2:大多数情况下,您需要在页面完全加载后执行JS,因此将其放在页眉中不会解决它,而在所有内容加载完成后再进行处理。

sometimes, javascript won't run unless you use a localhost, try using wamp/xamp etc. if this doesn't work 有时,除非您使用本地主机,尝试使用wamp / xamp等,否则javascript无法运行。如果这不起作用

  <html> <head> <title></title> </head> <body dir="rtl"> <p>Click the button to display an alert box:</p> <p><button onclick="myFunction()">Try it</button></p> <script> function myFunction() { alert("I am an alert box!"); } </script> </body> </html> 

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

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