简体   繁体   English

将html布局传输到joomla模板javascript中不起作用

[英]Transferring an html layout into a joomla template javascript doesn't work

I created a website layout through my basic notepad editor and the code works as a standalone html page. 我通过基本的记事本编辑器创建了一个网站布局,该代码可作为独立的html页面使用。 I tried transferring it to a joomla template, but the javascript code just isn't working. 我尝试将其传输到joomla模板,但是javascript代码无法正常工作。 It's for a menu that is mobile responsive. 它是用于移动响应菜单。

Here's the javescript code 这是javescript代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".nav-button").click(function () {
    $(".nav-button,.primary-nav").toggleClass("open");
    });    
});
</script>

Is there a special way I need to format it for the joomla template? 我是否需要为joomla模板设置格式的特殊方法?

I tried putting it in a .js file (js/main.js) that had this: 我尝试将其放在具有以下内容的.js文件(js / main.js)中:

$(document).ready(function(){
    $(".nav-button").click(function () {
    $(".nav-button,.primary-nav").toggleClass("open");
    });    
});

and then putting in the index.php file (I found this code from a similar question) 然后放入index.php文件(我从类似的问题中找到了此代码)

<?php 
defined( '_JEXEC' ) or die; 

$app             = JFactory::getApplication();
$doc             = JFactory::getDocument();
$user            = JFactory::getUser();
$this->language  = $doc->language;
$this->direction = $doc->direction;

//Load jQuery using Joomla's built in method
JHtml::_('jquery.framework');

// Define the template path
$template_path = 'templates/'  .$this->template;

// Le Javascript
$doc->addScript($template_path . '/js/main.js');

?>

It's still not working. 它仍然无法正常工作。 I would prefer to use the inline javascript code anyway, but really I just want it to work. 无论如何,我还是希望使用内联javascript代码,但实际上我只是希望它能工作。 What do I need to add to the javascript code to make it work? 我需要添加到javascript代码中才能使其正常工作吗?

Please try to add your javascript using the following code: 请尝试使用以下代码添加您的JavaScript:

JHtml::_('jquery.framework');
$doc->addScriptDeclaration('
    jQuery(document).ready(function(){
        jQuery(".nav-button").click(function () {
        jQuery(".nav-button, .primary-nav").toggleClass("open");
        });    
    });
');    

Note: Joomla loads jquery in noConflict mode so you have to change $ with jQuery in your jquery calls. 注意: Joomla以noConflict模式加载jquery,因此您必须在jquery调用中使用jQuery更改$

Reference: jQuery.noConflict() 参考: jQuery.noConflict()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM