可能重复:
使用jQuery从元素选择中获取属性值作为数组
我试图从元素列表中获取id
我有
<div class='test' id='123'> </div>
<div class='test' id='1243'> </div>
<div class='test' id='1223'> </div>
<div class='test' id='1423'> </div>
<div class='test' id='1223'> </div>
<div class='test' id='1253'> </div>
Run Code Online (Sandbox Code Playgroud)
我希望得到每个div的id
我有
var idarray=[];
var id= $('.test').attr('id'); //would only get the first 123 id.
idarray.push(id)
Run Code Online (Sandbox Code Playgroud)
在我的情况下,如何让每个id到idarray?谢谢您的帮助
当我单击按钮时,我试图选择下拉菜单到特定选项
我有
HTML
<select id='selectmenu'>
<option>
1
</option>
<option>
2
</option>
<option>
3
</option>
</select>
<a id='click'>click</a>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$('#click').click(function(){
var option=3;
//I want the select dropdown menu to select option 3 when I click the button.
//$('#selectmenu').select() //I think it will be something like this....
})
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
我无法使用div中的图像设置我的CSS.
我有
<div class='test'>
<a href='#'> <img src='haha.jpg'/> </a>
<a href='#'> <img src='imageINeed.jpg'/> </a>
</div>
<div class='test'>
<a href='#'> <img src='haha.jpg'/> </a>
<a href='#'> <img src='imageINeed1.jpg'/> </a>
</div>
<div class='test'>
<a href='#'> <img src='haha.jpg'/> </a>
<a href='#'> <img src='imageINeed2.jpg'/> </a>
</div>
Run Code Online (Sandbox Code Playgroud)
我下面的jquery将选择第二个图像
imageSize could be varies.
$('.test).each(function(){
$(this).find('img:eq(1)').width(imageSize);
})
Run Code Online (Sandbox Code Playgroud)
但是,有时我<a>在testdiv中只有1个标签.
<div class='test'>
<a href='#'> <img src='imageINeed2.jpg'/> </a>
</div>
Run Code Online (Sandbox Code Playgroud)
eq(1)img如果我只有1个<a>标签,则不会选择
我试过了
$('.test).each(function(){
$(this).find('img:nth-last-child').width(imageSize);
})
Run Code Online (Sandbox Code Playgroud)
它不会这样做.反正有没有做到这一点?非常感谢!
我试图找到具有id属性的最接近的元素并得到id.元素可以是<p>, <div>或其他元素.
我试过了
element=$('#test').closest('div[id]').attr('id');
Run Code Online (Sandbox Code Playgroud)
但如果元素是a,它只获取id div.我希望更加普遍.反正有没有这样做?非常感谢!
我试图td通过使用Jquery和获取表格文本javascript
我有以下内容
//tables contain bunch of tables
for(var i = 0; i < tables.length ; i ++){
var table = tables[i];
$(table 'td').each(function(){ //I know there is something wrong with my selector.
$(this).text()
})
Run Code Online (Sandbox Code Playgroud)
该jquery选择并不在我的情况下工作.如何为不同的表选择每个td?
谢谢您的帮助!
我试图在用户单击单词时创建不同的颜色状态
我的HTML
<a href='#' class='click' id='2'>
word
</a>
<a href='#' class='click' id='3'>
second word
</a>
Run Code Online (Sandbox Code Playgroud)
我想根据id切换文本背景.
例如,当用户单击word- >将背景颜色更改为黄色时再次单击 - >橙色并再次单击 - >原始(白色和透明).这是两个州.
第二个例子当用户点击second word- >将背景颜色改为黄色再次点击 - >橙色,再次点击 - >绿色再次点击 - >红色再次点击 - >(白色和透明)这是3个状态
颜色状态基于id属性.
我的代码就像
$('.click').click(function(){
var states = $(this).attr('id');
var classname = $(this).attr('class');
switch (classname){
case 'click':
$(this).attr('class', 'yellow');
$(this).css('background-color', 'yellow');
break;
case 'yellow':
$(this).attr('class', 'orange');
$(this).css('background-color', 'orange');
break;
case 'orange':
$(this).attr('class', 'red');
$(this).css('background-color', 'red');
break;
case 'red':
$(this).attr('class', 'click');
$(this).css('background-color', 'white');
break;
//add more …Run Code Online (Sandbox Code Playgroud)