javascript - 理解关于启动 jQuery 的一句话

标签 javascript jquery head

entire page 的定义是什么?在following line ?

When the entire page is included via PHP include() or require() function, make sure to put that script code inside the <body> tag and not inside the <head>, because calling jQuery script file from inside the tag doesn't work for some reason.

我在设置 jQuery 代码时遇到了问题。 但是,我设法通过将它放入 HEAD 来让它工作。和下面的代码

#1代码

$(document).ready(function(){     
    --- code here---- 
});

我还没有设法使任何功能正常工作。 我在代码 #1 的内部和外部都有它们。下面是一个例子。

#2 无效代码

    function notEmpty() {                        
        //put this in a function and call it when the user tries to submit
        var tags = document.getElementById('tags').value;
        if(tags == '' || tags == null) {         
            alert('Please enter one or more tags');
            return false;
        }
        return true;
    }    

我使用 PHP 通过以下代码在我的 index.php 中包含标签 HTML、HEAD、/HEAD 和 BODY。

#3 来源 HTML、HEAD 和 body 标签的代码

 include ( './official_content/html_head_body.php' );

最佳答案

我觉得这个说法很可疑。我从未遇到过任何问题,包括 <script> <head> 中的 jQuery 标记与其他文件一样<script>标签。浏览器无法感知 PHP 所做的任何事情。我怀疑这只是一个错误陈述。

为了解决您的特定代码,您在 #1 中编写的内容是完全正确的。将函数传递给 $(document).ready()允许您提供在文档“就绪”时运行的代码(意思是:当页面本身已加载时,不必加载外部资源,如图像、样式表等,您不需要等待) .

关于 #2,该函数根本没有使用 jQuery;它只使用内置的 JavaScript 函数。话虽如此,它应该可以通过使用 id="tags" 找到一个表单元素来正常工作。如果该元素为空 value,则显示警告框.你是怎么调用这个函数的?在 jQuery 的世界里,你会做这样的事情:

$("#id-of-your-form").submit(notEmpty);

你也可以重写 notEmpty()像这样利用 jQuery 的函数:

function notEmpty() {{
    var tags = $("#tags").val();
    if (tags == '' || tags == null) {
        alert('Please enter one or more tags');
        return false;
    }
    return true;
}

同样,该函数的其余部分当前完全连接到 jQuery。就这样就好了。

最后,#3 与 jQuery 无关,因为这是在 PHP 中发生的处理,而 JavaScript 完全不知道。

关于javascript - 理解关于启动 jQuery 的一句话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1314222/

相关文章:

正则表达式 : Same character part of two matches

javascript - LeafletJS 弹出窗口位置和标记添加和删除问题

带有 HTML DOM 的 JavaScript 不起作用

javascript - jQuery-UI slider 背景

php - 为什么元素显示在我的 <body> 而不是 <head>

mercurial - 在Mercurial退出后,如何判断是否需要 merge ?

javascript - 有时间限制的 HTML 文本

c# - 从 GET 更改为 POST jquery ajax 调用 c#

javascript - 使用 .html() 更新后,浏览器对于新行数的响应速度较慢。你能帮我看看我的错误吗?

python - 在不知道索引的情况下获取 Series 的第一个元素