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默认通过命名空间处理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
因此,您必须使用 $ 而不是 $ 。所以这是工作代码:
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>
我希望它有所帮助:)
PS:您可以通过更改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屋!