假设我有几个使用单个日期选择器的网页。 另外,我有另一个网页,我需要在同一页面上有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.
更多推荐
发布评论