【问题标题】:wordpress function.php jquery conflict?wordpress function.php jquery 冲突?
【发布时间】:2014-02-27 22:42:11
【问题描述】:

我正在制作一个 WordPress 主题

我正在试图弄清楚为什么我的所有脚本都不起作用...我在下面发布了我的完整 functions.php... 任何人都可以看到有什么问题吗?我怀疑 jquery 冲突...如果是这样,问题是什么?

如果您看不到下面的错误,请告诉我您是否需要问题所在的 url...

非常感谢!

<?php
function biscuits_enqueue() {

    wp_enqueue_script( 'jquery-ui', 'https://code.jquery.com/jquery.js', array( 'jquery') );
    wp_enqueue_script( 'bootstrap.min', get_template_directory_uri(). '/js/bootstrap.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'ekko-lightbox', get_template_directory_uri(). '/js/ekko-lightbox.js', array( 'jquery' ) ); 

}
add_action('wp_footer', 'biscuits_enqueue');
?>

<script>
    $(document).ready(function($){
        //lightbox
        $(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
            event.preventDefault();
            $(this).ekkoLightbox();
        });

        //bs-sidebar
        $(function () {
            var $window = $(window)
            var $body = $(document.body)
            var $sideBar = $('.bs-sidebar')

            var navHeight = $('.navbar').outerHeight(true) + 10

            $body.scrollspy({
              target: '.bs-sidebar',
              offset: navHeight
            })

            $('.bs-docs-container [href=#]').click(function (e) {
              e.preventDefault()
            })

            // back to top
            setTimeout(function () {
                    $sideBar.affix({
                    offset: {
                        top: function () {
                            var offsetTop = $sideBar.offset().top
                            var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10)
                            var navOuterHeight = $('.bs-docs-nav').height()

                            return (this.top = offsetTop - navOuterHeight - sideBarMargin)
                        },
                        bottom: function () {
                            return $('.bs-footer').outerHeight(true)
                        }
                    }
                })
            }, 100)

            $window.on('load resize', function () {
                $body.scrollspy('refresh')
            })

            $window.on('load', function () {
                $('.bs-top').affix();
                setTimeout(function () {
                    $sideBar.affix('checkPosition')
                }, 10);
            });

            // tooltip demo
            $('.tooltip-demo').tooltip({
                selector: "[data-toggle=tooltip]",
                container: "body"
            })

            $('.tooltip-test').tooltip()
            $('.popover-test').popover()

            $('.bs-docs-navbar').tooltip({
                selector: "a[data-toggle=tooltip]",
                container: ".bs-docs-navbar .nav"
            })
        })
    });
</script>

问题可能是由这个引起的:

【问题讨论】:

  • 您是否查看过任何浏览器控制台,您的代码会抛出哪个错误?
  • 错误信息说什么以及为什么 PHP 会与 jQuery 发生冲突?
  • php 不是这里的问题.. 除非您发现问题... 脚本可能... 这是控制台所说的 omarhabash.com/ss.png

标签: php jquery wordpress themes


【解决方案1】:

你在问什么? wp_enqueue_script 还是 javascript 部分?

wp_enqueue_script() 接受第四个参数,如果该参数为真,将在由get_footer()wp_footer() 生成的页脚中放置一个脚本。此外,您希望将wp_enqueue_scripts 用于脚本,而不是像这样使用wp_footer

add_action('wp_enqueue_scripts', 'my_theme_equeue_scripts');

..其实是fifth argument

。 . .

为了使用 jQuery,您必须使用 &lt;script&gt; 元素加载库并使用 src 属性指定脚本位置。

例如:

<script type="text/javascript" 
        src="http://rmko/wp-includes/js/jquery/jquery.js"></script>

<script type="text/javascript"
        src="http://rmko/wp-content/themes/MyWorkout24/js/bootstrap.js"></script>

如果您在 Wordpress 方面的所有操作都正确无误,最终 HTML 文档的顶部或底部附近应该会出现与上述类似的一行。如果您在最终的 HTML 文档中找不到“jquery.js”,请查看此答案的前一部分。请注意,“bootstrap.js”必须在 jQuery 之后(更接近最终 HTML 的底部)。

如果符合要求,您可以使用通常的自动执行匿名函数范例将“特殊”美元符号传递给 JavaScript 代码块;

<script>
(function($){

/*
we define a function with one "special" argument [function($){...}] and call
it immediately in another function: [()(jQuery)]. The outer function is given
the global jQuery object as an argument. That global variable is defined in 
"jquery.js". 

This works less than perfectly if -this- script is "below" (in the footer) 
everything else such as elements with HTML id attributes and CSS classes
that are commonly used as jQuery selectors. To  be on the safe side you
can do this: 
*/ 

// here, $ means jQuery. 
$(document).ready(function($){

    $('input[type="submit"]').click(function(event){
        event.preventDefault();
        alert('form is ready to be submitted.');
    });

});


})(jQuery)
</script>

。 . .

Omar,我推测您是 WP/PHP/JS 的新手,欢迎您使用 StackOverflow。但是,在此处发布问题之前,请先阅读介绍性文档,因为我将这个地方视为一个可以找到重要解决方案和针对难题的书面建议的地方。

这也是一个通过阅读问题来学习的好地方。不幸的是,我担心没有人会从你的问题或我的回答中学到任何他们无法通过浏览网页甚至一本书学到的东西。我希望您能通过一些琐碎的问题理解我对 SO 污染的担忧。

【讨论】:

  • javascript 部分是我认为是错误的...对不起,我的问题可能不清楚,因为这是我的第一个 wordpress 网站...我通过对默认站点建模...我知道存在问题,因为 dom 没有读取我的脚本……看看控制台正在读取什么。 omarhabash.com/ss.png
猜你喜欢
  • 1970-01-01
  • 2012-05-05
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多