javascript JQuery 不适用于 Joomla 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18548483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:21:46  来源:igfitidea点击:

JQuery not working on Joomla 3

javascriptjqueryjoomla

提问by Tirpox

I am trying to make a simple jQuery script work under Joomla 3. Here is what my module looks now:

我正在尝试在 Joomla 3 下制作一个简单的 jQuery 脚本。这是我的模块现在的样子:

<?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>

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 Easy 插件,但没有成功。

回答by Tirpox

Ok, finally after a lot of research I have the answer. Because the Joomla is handling jQuery through namespacingby 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

好的,经过大量研究,我终于有了答案。因为 Joomla默认通过命名空间处理 jQuery ,所以你必须把它放在你的 jQuery 代码中。它也在这里提到: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:

因此,您必须使用jQuery而不是使用$。所以这是工作代码:

<?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> 

I hope it helps :)

我希望它有帮助:)

P.S: You can turn the namespacing off by changing the jQuery declaration like so

PS:您可以通过像这样更改 jQuery 声明来关闭命名空间

JHtml::_('jquery.framework', false);

Please note that this wasn't working for me...

请注意,这对我不起作用......