JQuery 不适用于 Joomla 3 [英] JQuery not working on Joomla 3
问题描述
我正在尝试在 Joomla 3 下制作一个简单的 jQuery 脚本.这是我的模块现在的样子:
I am trying to make a simple jQuery script work under Joomla 3. Here is what my module looks now:
<?php
// no direct access
defined('_JEXEC') or die;
$doc = JFactory::getDocument();
JHtml::_('jquery.framework');
$doc->addScriptDeclaration('
$(document).ready(function () {
$(".text").text("By this");
});
');
?>
<div class="text">Text should be changed...</div>
我在普通页面上尝试了此代码段并且运行良好,只是我不知道为什么它不想在 Joomla 中运行.我也安装了 jQuery Easy 插件,但没有成功.
I tried this snippet on a normal page and works perfectly, just I don't know why it does not want to work in Joomla. I installed the jQuery Easy plugin as well, but with no success.
推荐答案
好的,经过大量研究我终于有了答案.因为 Joomla 默认通过 namespacing 处理 jQuery,所以你必须把它放在你的 jQuery 代码中.这里也提到:http://docs.joomla.org/J3.1:Javascript_Frameworks#jQuery_JavaScript_Framework
Ok, finally after a lot of research I have the answer. Because the Joomla is handling jQuery through namespacing by default, you have to put it in your jQuery code. It mentions here as well: http://docs.joomla.org/J3.1:Javascript_Frameworks#jQuery_JavaScript_Framework
因此,您必须使用 jQuery 而不是使用 $.所以这是工作代码:
So instead of using $ you have to use jQuery. So here is the working code:
<?php
// no direct access
defined('_JEXEC') or die;
$doc = JFactory::getDocument();
JHtml::_('jquery.framework');
$doc->addScriptDeclaration('
jQuery(document).ready(function () {
jQuery(".text").text("By this :)");
});
');
?>
<div class="text">Text should be changed...</div>
我希望它有帮助:)
P.S:您可以通过像这样更改 jQuery 声明来关闭命名空间
P.S: You can turn the namespacing off by changing the jQuery declaration like so
JHtml::_('jquery.framework', false);
请注意,这对我不起作用...
Please note that this wasn't working for me...
这篇关于JQuery 不适用于 Joomla 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!