是否可以在页面正文中加载Jquery

发布于 2024-09-17 11:10:12 字数 687 浏览 8 评论 0原文

我正在使用 code-igniter,并且我的一些视图需要 jquery。因为它们必须在多个地方使用,所以它们必须在文件中调用 jquery,但是由于它们引用外部文件,因此在加载 jquery 之前会评估对 $(document.ready) 的调用,因此会失败。是否可以将 jquery 放入正文中并在评估 javascript 之前仍然加载它。或者,有某种方法可以通过 code-igniter 将需要 jquery 的事实传递回标头,这些标头是在相关文件之前调用的。

在视图中:

echo $this->import->js('jquery.js','jquery');
echo '<script type="text/javascript">
$(document).ready(function(){$(\'div#login.rounded\').corner();})
</script>';

您可以在以下位置查看页面: http://formulator.codingproject.net/content/ login/

注意 该页面实际上驻留在我的家用计算机上,因此预计验证码会失败。

I am using code-igniter, and some of my views require jquery. Because they must be used in multiple places they must call jquery in their file, however since they are referencing an external file, calls to $(document.ready) are evaluated before loading jquery and therefore fail. Is it possible to put jquery in the body and still have it load before an javascript is evaluated. Or alternatively, is the some way to pass the fact that jquery is required back through code-igniter into the headers, which were callled before the file in question.

In a view:

echo $this->import->js('jquery.js','jquery');
echo '<script type="text/javascript">
$(document).ready(function(){$(\'div#login.rounded\').corner();})
</script>';

You can view the page at: http://formulator.codingproject.net/content/login/

NOTE This page actually resides on my home machine, so it is expected that the recaptcha fails.

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

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

发布评论

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

评论(6

谷夏 2024-09-24 11:10:12

我想答案是肯定的。您可以在 body 中加载 jQuery.js。但是你必须只在 jQuery.js 声明之后编写脚本标签,否则你可能会遇到错误:)

PS:如果我错了,请纠正我: )

I guess the answer is yes. you can load the jQuery.js in your body. But you have to write your script tags only after jQuery.js declaration, if not you may end up with errors :)

PS : please correct me If I'm wrong :)

稀香 2024-09-24 11:10:12

jQuery 确实应该在 head 元素中调用。这是有条件地执行此操作的方法(未经测试)。

在你的控制器中,每个需要 jQuery 的函数都应该有:

$data['need_jquery'] = true;
$this->load->view('header');

在你的标题视图中:

<head>
    <? if($need_jquery) { ?>
        <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" /></script>
    <? } ?>
</head>

jQuery should really be called in the head element. Here's how you'd do that conditionally (untested).

In your controller, each function that needs jQuery should have:

$data['need_jquery'] = true;
$this->load->view('header');

In your header view:

<head>
    <? if($need_jquery) { ?>
        <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" /></script>
    <? } ?>
</head>
妄司 2024-09-24 11:10:12

看起来你正在使用PHP?如果是这样,请创建一个返回该字符串的静态方法,但前提是该字符串尚未包含在该请求中。然后您可以确保它只被包含一次。

It looks like you are using PHP? If so, create a static method that returns that string, but only if it hasn't already been included this request. Then you can ensure that it's only being included once.

帅哥哥的热头脑 2024-09-24 11:10:12

我的网站也是这样。我所做的就是在所有页面上加载一个标题。在该标头中,我执行 if($this->uri->segment(2) == 'controller')。然后,如果该控制器需要,我会加载 jQuery 和某些脚本。

My website is the same way. What I do is I have one header that is loaded on all pages. In that header I do if($this->uri->segment(2) == 'controller'). Then I load jQuery and certain scripts if needed for that controller.

滥情稳全场 2024-09-24 11:10:12

我认为如果每个页面的标签中都包含jQuery就可以了,此外,你可以使用jQuery的缩小版,它不是那么重。

I think it will be fine if jQuery will be included in the tag of every page, besides, you can use the minified version of jQuery which is not so heavy.

挥剑断情 2024-09-24 11:10:12

呃,也许我错了。但是,当我查看您的源代码并跟踪 jquery 文件的位置时: http:// /www.formulator.com/assets/scripts/jquery/jquery.js 我收到“找不到页面”错误。所以我猜这可能是问题所在。也许您的 php 输出方式不包含正确的域/子域?

Uh, maybe I'm wrong. But when I view your source code and follow where the jquery file is: http://www.formulator.com/assets/scripts/jquery/jquery.js I get a "page cannot be found" error. So I'm guessing this could be the problem. Maybe the way your php outputs it isn't including the correct domain/subdomain?

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