【问题标题】:Understanding a sentence about starting up jQuery了解启动jQuery的一句话
【发布时间】:2010-11-21 19:15:40
【问题描述】:

following lineentire page的定义是什么?

当通过 PHP include() 或 require() 包含整个页面时 函数,确保把那个脚本 <body> 标签内的代码,而不是 在<head> 里面,因为调用 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' );

【问题讨论】:

  • 好问题。文档在这里不是很具体。
  • 感谢您的回答! - 因为很好的解释,我接受了 Votey 的回答。

标签: javascript jquery head


【解决方案1】:

我觉得那句话很可疑。我从来没有遇到过任何问题,包括文档的<head> 中的jQuery 的<script> 标记,就像任何其他<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 完全不知道。

【讨论】:

  • 我同意。从来没有见过这个问题,发现“句子”有问题
【解决方案2】:

将 jQuery 源代码放在 html_head_body.php 文件中,并将您的函数添加到您的“内容”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>

【讨论】:

  • 我现在有两个 $(document).ready(function(){:一个在 HEAD,一个在 BODY。 -- 这会是个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 2017-05-15
  • 2019-11-21
  • 1970-01-01
  • 2016-10-20
相关资源
最近更新 更多