如何管理不同页面和需求之间的java脚本引用(How to manage the java scripts references between different pages and needs)

编程入门 行业动态 更新时间:2024-10-25 10:21:24
如何管理不同页面和需求之间的java脚本引用(How to manage the java scripts references between different pages and needs)

假设我有几个使用单个日期选择器的网页。 另外,我有另一个网页,我需要在同一页面上有3个日期选择器。

那么我该如何管理java脚本id引用呢? 我应该在_layout.xml使用这样的单个脚本吗?

<script> $(document).ready(function () { $("#datepicker1").datepicker(); $("#datepicker2").datepicker(); $("#datepicker3").datepicker(); }); </script>

所有页面(除了多个页面)都将具有:

<div id="datepicker1"></div>

并且倍数将具有:

<div id="datepicker1"></div> <div id="datepicker2"></div> <div id="datepicker3"></div>

我来自OO编程,有些东西不适合这里。 web \ js世界中对这个常见用例的约定是什么?

编辑:mmultiple datepicker页面脚本有不同的设置...

Let's say I have couple of web pages where a single datepicker is used. In addition, I have another web page where I need 3 datepickers on the same page.

So how do I manage the java script id reference? Should I have single script in _layout.xml with something like this?

<script> $(document).ready(function () { $("#datepicker1").datepicker(); $("#datepicker2").datepicker(); $("#datepicker3").datepicker(); }); </script>

And all pages (besides the multiple) will have :

<div id="datepicker1"></div>

and the multiple will have:

<div id="datepicker1"></div> <div id="datepicker2"></div> <div id="datepicker3"></div>

I come from OO programming and something doesn't fit here. What are the conventions in web\js world to this common use case?

Edit: the mmultiple datepicker page script has different settings...

最满意答案

这取决于。 如果它们都使用完全相同的设置,请使用类:

<div class="datepicker"></div>

然后

$('.datepicker').datepicker();

无论有多少(一,三,一万[虽然这可能使页面速度极慢]),这将处理类上的datepicker所有内容。

如果他们需要不同的设置,那么您需要坚持使用ID,并将其全部初始化,就像您在问题中所示。

It depends. If they all use exactly the same settings, use a class:

<div class="datepicker"></div>

then

$('.datepicker').datepicker();

That will handle everything with the class datepicker on it, no matter how many there are (one, three, ten thousand [though that might make the page incredibly slow]).

If they need different settings then you'll need to stick with IDs, and initialise them all individually, as you've shown in the question.

更多推荐

本文发布于:2023-08-05 16:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1433957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   需求   页面   references   pages

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!