Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery";单击“按id”时;在Wordpress中无法运行_Javascript_Jquery_Wordpress_Jquery Events - Fatal编程技术网

Javascript jQuery";单击“按id”时;在Wordpress中无法运行

Javascript jQuery";单击“按id”时;在Wordpress中无法运行,javascript,jquery,wordpress,jquery-events,Javascript,Jquery,Wordpress,Jquery Events,我读过很多类似的问题,并尝试实施解决方案,但没有一个适合我 这是我在页面中的代码: <button id="hello" type="button">Try it</button> 我能让这段代码正常工作的唯一方法是将函数调用直接放入按钮中,如下所示: <button id="hello" type="button" onclick="hello();">Tr

我读过很多类似的问题,并尝试实施解决方案,但没有一个适合我

这是我在页面中的代码:

<button id="hello" type="button">Try it</button>
我能让这段代码正常工作的唯一方法是将函数调用直接放入按钮中,如下所示:

<button id="hello" type="button" onclick="hello();">Try it</button>
$(document).ready(function() {
    $('#hello').click(function(){
        alert('inside hello function');
    });
});
试试看
但问题是Wordpress会在我切换到可视化页面编辑器时立即删除这些内容


任何帮助都将不胜感激

应使用$(文档)封装代码。就绪事件:

$(document).ready(function() {
    $('#hello').click(hello);
    function hello(){
        alert('inside hello function');
    };
});
对于这种事件处理,您确实不需要命名函数,您可以这样编写代码:

<button id="hello" type="button" onclick="hello();">Try it</button>
$(document).ready(function() {
    $('#hello').click(function(){
        alert('inside hello function');
    });
});

你能试试
jQuery
而不是
$
?按钮是动态创建的吗?请确保id保持一致并使用:$('body')。在('click','#hello',函数(e){return hello();})上;如果jquery是以$(某些代码)的形式编写的,那么它就不能在Wordpress中工作,而应该是jquery(某些代码)。我完全同意重复标记,因为我们需要这个问题的另一个答案?Wordpress使用jquery兼容模式,$将无法工作,除非您将其作为闭包的引用传递:(函数($){//your code here})(jQuery);