jQueryfunction无法在wordpress中工作
我正在尝试在wordpress中运行一个简单的jQuery函数来隐藏和显示内容,具体取决于下拉列表,代码在wordpress之外工作正常,但是当我将它添加到我的子主题时显示所有内容。 我检查了标头并正确加载了jQuery文件。
jQuery(document).ready(function() { $('#id_BJ').closest('article').addClass('city_table').hide(); $('#id_SH').closest('article').addClass('city_table').hide(); $('#id_SZ').closest('article').addClass('city_table').hide(); $('#id_chinese_city').change(function() { $('article.city_table').hide(); $('#id_' + $(this).val()).closest('article').show(); }); });
Choose first Beijing Shanghai Shenzhen Beijing
Shanghai
Shenzhen
我也试过使用标签,但结果是一样的
任何想法为什么会这样?
我想这可能是因为你在安全模式下使用$ with jquery,你能做什么就像这样改变你的function
jQuery(document).ready(function($) { $('#id_BJ').closest('article').addClass('city_table').hide(); $('#id_SH').closest('article').addClass('city_table').hide(); $('#id_SZ').closest('article').addClass('city_table').hide(); $('#id_chinese_city').change(function() { $('article.city_table').hide(); $('#id_' + $(this).val()).closest('article').show(); }); });
它应该允许你传递$,它应该使你的function工作。
看看我的文件。已经与你的线对比,看看我在说什么。
更改
jQuery(document).ready(function() {
至:
jQuery(document).ready(function($) {
WordPress默认以安全模式运行jQuery。 更改上面的行,您应该可以正常使用代码中的$
。
另一种选择如下:
(function($) { //Code here })( jQuery );
WordPress在noConflict模式下使用jQuery。
以上解决方案应该有效
但是,如果您查看APPI文档,这应该更清楚 – https://api.jquery.com/jquery.noconflict/