理解一句关于启动jQuery的句子

发布于 2024-08-02 03:35:28 字数 1082 浏览 5 评论 0原文

下面一行中整个页面的定义是什么?

当通过 PHP include() 或 require() 包含整个页面时 函数,请确保放置该脚本 标签内的代码而不是 在 内,因为调用 来自内部的 jQuery 脚本文件 标签对某些人不起作用 原因。

我在设置 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' );

What is the definition of entire page in the 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.

I have had problems in setting up a jQuery codes.
However, I managed to get one to work by putting it inside HEAD and the following code

#1 code

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

I have not managed to get any functions to work.
I have had them inside and outside the code #1. The following is an example.

#2 Not working code

    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;
    }    

I use PHP to include the tags HTML, HEAD, /HEAD and BODY by the following code in my index.php.

#3 Code by which sources HTML, HEAD and body tags

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深者入戏 2024-08-09 03:35:28

我觉得这个说法很可疑。 我从未遇到过任何问题,包括文档 中 jQuery 的

为了解决您的特定代码,您在 #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 完全不知道这一点。

I find that statement suspect. I've never encountered any problem including the <script> tag for jQuery in the <head> of the document, like any other <script> tag. The browser cannot be aware of anything that PHP has done. I suspect it is simply a misstatement.

To address your specific code, what you've written in #1 is entirely correct. Passing a function to $(document).ready() lets you supply code to be run when the document is "ready" (meaning: when the page itself has loaded, without necessarily having loaded external resources like images, stylesheets, et cetera, for which you wouldn't want to have to wait).

Regarding #2, that function is not using jQuery at all; it's using only built-in JavaScript functions. Having said that, it should work correctly by finding a form element with id="tags" and showing an alert box if that element has an empty value. How were you calling this function? In the world of jQuery, you'd do something like this:

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

You could also rewrite the notEmpty() function to exploit jQuery like so:

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

Again, the rest of that function currently connects to jQuery at all. It's fine just the way it is there.

Finally, #3 is unrelated to jQuery, since that's processing happening within PHP, of which JavaScript remains entirely unaware.

蓝梦月影 2024-08-09 03:35:28

将 jQuery 源代码放入 html_head_body.php 文件中,并将函数添加到“content”php 文件中。

<html>
<head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
   <script type="text/javascript">
     $(document).ready(function(){

       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;
       }

     });
   </script>         
</body>
</html>

Put the jQuery source in the html_head_body.php file and add your function in your "content" php file.

<html>
<head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
   <script type="text/javascript">
     $(document).ready(function(){

       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;
       }

     });
   </script>         
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文