【问题标题】:How to include custom jQuery / javascript in magento 1.8如何在 magento 1.8 中包含自定义 jQuery / javascript
【发布时间】:2018-05-02 11:51:08
【问题描述】:

我有一个自定义的 javascript 和 jQuery 代码。

我是 magento 新手,需要知道如何将这些 jquery 和 JS sn-p 添加到我的 magento 中,以便它识别这些。

我试图在页面加载时显示工具提示,并在一段时间后使用以下 JS 小提琴链接

http://jsfiddle.net/Lvzuz/17/

小提琴使用以下文件:

1) jQuery 2) bootstrap.js 3) bootstrap.css

因此,我已将我的 magento 文件更新为:

app\design\frontend\rau\default\template\page\html\pager.phtml

<?php if($followupbuttonshow){?>
    <div class="thiscategory">
        <a class="followlink" href="javascript:void" rel="tooltip" data-original-title="Place your stuff here"> <?php echo $this->__('Follow'); ?></a>  
    </div>
<?php } ?>

更新:

app\design\frontend\rau\default\layout\local.xml

<reference name="head">
      <action method="addJs"><js>jquery/jquery.js</js></action>
      <action method="addJs"><js>jquery/jquery.noconflict.js</js></action>
      <action method="addLinkRel"><rel>text/javascript</rel><href>https://netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.js</href></action>
      <action method="addLinkRel"><rel>text/css</rel><href>https://netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.css</href></action>
   </reference>
...

app\design\frontend\rau\default\layout\page.xml

<default translate="label" module="page">
   <action method="addJs"><script>js/custom.js</script></action>

rauqa\skin\frontend\rau\default\js\custom.js

jQuery(document).ready(function () {
    console.log('tooltipp js....');
    $j('.thiscategory .followlink').tooltip().eq(0).tooltip('show').tooltip('disable').one('mouseout', function() {
    $j(this).tooltip('enable');
    });

setTimeout(function() {
$j('.thiscategory .followlink').tooltip().eq(0).tooltip('hide').tooltip('enable');
}, 5000);

});

控制台出错:

工具提示 js....

tooltip.js?q=29032018:3 Uncaught TypeError: $j(...).tooltip is not a function

在 HTMLDocument。 (tooltip.js?q=29032018:3)

在 j (jquery.min.js?q=29032018:2)

在 Object.fireWith [as resolveWith] (jquery.min.js?q=29032018:2)

在 Function.ready (jquery.min.js?q=29032018:2)

在 HTMLDocument.J (jquery.min.js?q=29032018:2)

验证js是否加载如下截图:

custom.js 文件已加载

已加载 jquery - 不确定这是否是实际的 jquery 文件

这是唯一加载的 jquery

用于 bootstrap.js 和 css 加载的 CDN

链接到 jquery :

问题:

不确定我是否以正确的方式正确包含了所有文件(html、xml)

请指出问题出在哪里。以及我必须包含脚本以供 magento 识别的方式。

【问题讨论】:

  • ... 到底是什么问题?除了 Magento 使用 Prototype 而不是 jQuery(取决于版本)这一事实。

标签: javascript php magento


【解决方案1】:

您只需将粘贴的script 放入 phtml 文件中,它应该可以工作。如果 jquery 没有链接到页面,则链接它。另外,请注意 jquery 变量的命名。在许多情况下,它的名称是$,但您在script 中将其用作jQuery

【讨论】:

  • 已经更新了@Lajos 的问题,删除了'jQuery',只是使用了 $..i 得到错误 Uncaught TypeError: $(...).ready is not a function
  • 您经常在 Magneto 中使用 jQuery,因为 Prototype 也使用 $ ... Magento 的兼容性套件将 $j 分配给 jQuery IIRC。
  • @stack200s 请说明您是如何将 jquery 链接到您的页面的。
  • @stack200s 我在您的问题中看不到您加载 jquery 的确切方式。您将需要类似 的内容,我认为您的代码中缺少此内容。
  • @stack200s 你需要有一个 jquery 的副本,可能在 rauqa/skin/frontend/rau/default/js 的文件夹中,或者从在线资源链接它,例如 code.jquery.com/jquery-3.3.1.min.js .无论哪种方式,您都需要一个脚本标签,就像您的问题图片中显示的脚本标签一样。
【解决方案2】:

我相信您的 JavaScript 文件缺少关闭: );

编辑新错误:$ 是 Magento 中的原型,但您在代码中使用 jQuery。如果您添加了 jQuery,请使用分配给它的别名(例如 $j)。或者:

jQuery(document).ready(function(){
    console.log('tooltipp....');
    jQuery('.thiscategory .followlink').tooltip().eq(0).tooltip('show').tooltip('disable').one('mouseout', function() {
        jQuery(this).tooltip('enable');
    });

    setTimeout(function() {
        jQuery('.thiscategory .followlink').tooltip().eq(0).tooltip('hide').tooltip('enable');
    }, 5000);
});

【讨论】:

  • 请检查我更新的问题,即使用 $j 之类的别名替换了 jQuery,我仍然得到同样的错误
  • 如果您在浏览器中查看页面的代码,是否存在 tooltip.js?如果是这样,它是否实际加载(不是 404)?
  • 这是哪个 tooltip.js 库?如果是 Bootstrap,你还需要 Bootstrap JS。
  • 该示例使用 Bootstrap 及其工具提示。没有 tooltip.js,只有 jQuery 和 Bootstrap。 jsfiddle上的查看框架源显示它。查看源代码:fiddle.jshell.net/apougher/Lvzuz/show/
  • 在最后一个小提琴中,JS 和 CSS 添加了“http://”,导致它们无法加载,因为小提琴使用了“https://”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多