[英]javascript variable for jquery script
I dont know Javascript at all, so sorry for asking a question like this... 我根本不懂Javascript,很抱歉问这样的问题......
This is what I have: 这就是我所拥有的:
$(document).ready(function(){$("#more0").click(function(){$("#update0").slideToggle("normal");});});
$(document).ready(function(){$("#more1").click(function(){$("#update1").slideToggle("normal");});});
$(document).ready(function(){$("#more2").click(function(){$("#update2").slideToggle("normal");});});
$(document).ready(function(){$("#more3").click(function(){$("#update3").slideToggle("normal");});});
$(document).ready(function(){$("#more4").click(function(){$("#update4").slideToggle("normal");});});
$(document).ready(function(){$("#more5").click(function(){$("#update5").slideToggle("normal");});});
$(document).ready(function(){$("#more6").click(function(){$("#update6").slideToggle("normal");});});
$(document).ready(function(){$("#more7").click(function(){$("#update7").slideToggle("normal");});});
$(document).ready(function(){$("#more8").click(function(){$("#update8").slideToggle("normal");});});
$(document).ready(function(){$("#more9").click(function(){$("#update9").slideToggle("normal");});});
$(document).ready(function(){$("#more10").click(function(){$("#update10").slideToggle("normal");});});
And So On.. Until #more30 and #update30...
So... Right now, my pages has 30 lines :) 所以...现在,我的页面有30行:)
Is there a way to do it less complicated? 有没有办法减少复杂性?
Thanks! 谢谢!
Use attribute selector ^=
. 使用attribute selector ^=
。 The [attribute^=value]
selector is used to select elements whose attribute value begins with a specified value. [attribute^=value]
选择器用于选择属性值以指定值开头的元素。
$(document).ready(function(){
$("[id^='more']").click(function(){
$("#update" + $(this).attr('id').slice(4)).slideToggle("normal");
});
});
Try to use attribute starts with selector
to select all the elements having id starts with more
, then extract the numerical value from it using the regular expression and concatenate it with update
to form the required element's id and proceed, 尝试使用attribute starts with selector
来选择id为more
所有元素,然后使用正则表达式从中提取数值,并将其与update
连接以形成所需元素的id并继续,
$(document).ready(function(){
$("[id^='more']").click(function(){
var index = $(this).attr('id').match(/\d+/)[0];
$("#update" + index).slideToggle("normal");
});
});
use attribute start with selector 使用属性start和selector
$(document).ready(function(){
$("[id^='more']").click(function(){
$("[id^='update']").slideToggle("normal");
});
});
Well first of all, you could replace the multiple ready event handler registrations with just one, eg 首先,您可以用一个替换多个就绪事件处理程序注册,例如
$(document).ready(
$("#more0").click(function(){$("#update0").slideToggle("normal");});
//...
);
Then, since your buttons/links has pretty much the same functionality, I would recommend merging these into a single click event handler registration as such: 然后,由于您的按钮/链接具有几乎相同的功能,我建议将这些功能合并到单击事件处理程序注册中:
$(document).ready(
$(".generic-js-hook-class").click(function(){
var toggleContainer = $(this).data('toggleContainer');
$(toggleContainer).slideToggle("normal");
});
);
The above solution uses HTML Data Attributes to store information on which element to toggle and requires you to change the corresponding HTML like so: 上面的解决方案使用HTML数据属性来存储关于要切换的元素的信息,并要求您更改相应的HTML,如下所示:
<div class=".generic-js-hook-class" data-toggle-container="#relatedContainer">Click me</div>
<div id="relatedContainer>Toggle me</div>
I would recommend you to use Custom Data Attributes (data-*) . 我建议你使用自定义数据属性(data- *) 。 Here You can store which element to toggle in the data attributes which can be fetched and used latter. 这里您可以存储要在数据属性中切换的元素,这些属性可以在后面获取和使用。
JavaScript , In event-handler you can use .data() to fetch those values. JavaScript ,在事件处理程序中,您可以使用.data()来获取这些值。
$(document).ready(function () {
$(".more").click(function () {
$($(this).data('slide')).slideToggle("normal");
});
});
HTML HTML
<div class="more" data-slide="#update1">more1</div>
<div class="more" data-slide="#update2">more2</div>
<div id="update1">update1</div>
<div id="update2">update2</div>
//select all elements that contain 'more' in their id attribute.
$('[id^=more]').click(function(){
//get the actual full id of the clicked element.
var thisId = $(this).attr("id");
//get the last 2 characters (the number) from the clicked elem id
var elemNo= thisId.substr(thisId.length-2);
//check if last two chars are actually a number
if(parseInt(elemNo))
{
var updateId = "#update"+elemNo;//combine the "#update" id name with number e.g.5
}
else
{
//if not, then take only the last char
elemNo= thisId.substr(thisId.length-1);
var updateId = "#update"+elemNo;
}
//now use the generate id for the slide element and apply toggle.
$(updateId).slideToggle("normal");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.