具有相同前缀ID的div的jQuery帮助 [英] jquery help for div having same prefix id

查看:150
本文介绍了具有相同前缀ID的div的jQuery帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个具有不同ID的DIV(但具有相同的前缀(testDiv [1 | 2 | 3 | 4])),并且我想在某些事件中设置其可见性(可见或隐藏). 我该如何一次将DIV可见性属性设置为

I have 4 DIV's with different ID's (but have same prefix (testDiv[1|2|3|4]) ) and i want to set their visblity(visible or hidden) on some event. How can i use to set the DIV visiblity property at one times something like

$('testDiv*').css('visibility', 'visible'); 
OR
$('testDiv*').css('visibility', 'hidden');

有解决此问题的方法.我可以为所有DIV创建一个名为".comnClass"的类,并更改其属性 但是我想要一个具有四个不同DIV ID的解决方案.

There is workaround to this problem. I can have a class named ".comnClass" for all the DIV's and change its property But i want a solution having four different DIV id.

谢谢所有

推荐答案

您可以使用以选择器^= 开头,如下所示:

You can use the starts with selector ^= like this:

$('div[id^=testDiv]').css('visibility', 'visible'); 

说明:选择要 具有带有 以给定值开头的值 字符串.

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

注意 ,如果您改用类,则可以轻松地将它们作为目标,但这取决于您的要求.

Note that if you use a class instead, you can target them easily but that depends on your requirements.

这篇关于具有相同前缀ID的div的jQuery帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆