javascript - 将 javascript 插入到 ckeditor 的正文中

标签 javascript ckeditor4.x

我使用 CKEDITOR 4.6

我将此代码(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>

但是当我点击按钮时,这个 java 脚本代码不起作用? enter image description here enter image description here

如何在 CKEDITOR 中运行 javascript?

我也设置了这个配置但还是不行

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
        }
    };
};

最佳答案

你的代码有一些问题:
1:你不应该放 <script><p> 中标记元素。
2: 大多数时候,你需要在页面完全加载后执行你的 JS,所以将它放在页眉中并不能解决问题,在你的所有内容加载完毕后放置。

有时,除非您使用本地主机,否则 javascript 不会运行,尝试使用 wamp/xamp 等。如果这不起作用

    <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>

关于javascript - 将 javascript 插入到 ckeditor 的正文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40780285/

相关文章:

javascript - Google Chrome 版本 34.0.1847.116 m 不再传递单选按钮的选定值

javascript - 我需要一个脚本来检测所有浏览器上的所有长按或连续按键

CKeditor - 有什么方法可以在编辑器中使用实时复选框吗?

javascript - 是否可以在 NodeJS 中模拟键盘/鼠标事件?

javascript - redux-thunk:暂停组件执行直到 Action Creator 完成

javascript - Gatsby - Javascript 替换来自 API 响应的图像 src

symfony - WebPack Encore 中缺少带有 CKEditor 的工具栏项目

javascript - 当我进入源代码 View 时,如何跳转到 CKEditor 4 中的当前位置?

css - 如何更改ck editor 4的边框颜色、半径和粗细

javascript - CKEditor 奇怪的样式改变行为