[英]WordPress jQuery working but not the alert function
I am working on a WordPress plugin and I've included jQuery. 我正在研究WordPress插件,并且包含jQuery。 For some very strange reason the
alert
function is not being shown. 由于某些非常奇怪的原因,未显示
alert
功能。 See code below: 参见下面的代码:
$("#my_button").click(function() {
alert('before hide/show');
$('#divA').hide();
$('#divB').show();
alert('after hide/show');
});
In the code above divA
is actually hidden and divB
is shown, so it means jQuery is working, but strangely enough none of the alert
are being shown. 在上面的代码
divA
实际上是隐藏的, divB
所示,所以这意味着jQuery 是工作, 但没有任何的奇怪的alert
被显示。
I am running the code from the plugin options page in the backend. 我正在从后端的插件选项页面运行代码。
Why is this happening? 为什么会这样呢?
The problem was Firefox Cache. 问题是Firefox缓存。 It was the last thing I expected it to be but after doing an Alt F5 it started working.
这是我所期望的最后一件事,但是在完成Alt F5之后它开始工作。
Not sure, if it's Wordpress issue or just the FF browser but this sorted it for me. 不确定是Wordpress问题还是FF浏览器,但这对我来说还是不错的选择。
Can't believe it was the most simple thing :o/ Lesson learned... If the code doesn't work and it's not making sense, press ALT F5 First. 简直不敢相信这是最简单的事情:o /经验教训...如果代码不起作用并且没有意义,请先按ALT F5。
Have you called any jQuery file 您是否调用过任何jQuery文件
Try by adding following before your code 尝试在代码之前添加以下内容
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
The alert function is override by some other code. 警报功能被其他一些代码覆盖。 That 's the reason why it doesn't show the alert box.
这就是为什么它不显示警报框的原因。
You can look at this example then you can understand what I mean 您可以看一下这个例子,然后您就可以理解我的意思了
function alert(param)
{
// do nothing
}
alert("Call message"); // this will not show up anything because the system function is override
I had same problem. 我有同样的问题。 It was not doing a simple alert.
它不是在做一个简单的警报。 The way I solved was just including jQuery.(document).ready after
wp_enqueue_script
like this: 我解决的方法就是在
wp_enqueue_script
之后像这样包含jQuery。(document).ready:
<?php
add_action( 'wp_enqueue_script', 'load_jquery' );
function load_jquery() {
wp_enqueue_script( 'jquery' );
}
?>
//call jQuery
jQuery.(document).ready(function(){
alert('test');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.