做getElementsByClassName(等类似的功能getElementsByTagName和querySelectorAll)的工作方式相同getElementById,还是他们返回元素的数组?
我问的原因是因为我试图改变所有元素的样式getElementsByClassName.见下文.
//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';
//works
document.getElementById('myIdElement').style.size = '100px';
Run Code Online (Sandbox Code Playgroud) 使用jQuery如何选择单个子元素?我查看了Traversing API并知道我可以选择所有直接子img元素,如下所示:
$(this).children('img');
Run Code Online (Sandbox Code Playgroud)
要选择第一个子img元素,我可以使用这样的下标:
$(this).children('img')[0];
Run Code Online (Sandbox Code Playgroud)
但我想我有点惊讶我不能这样做:
$(this).child('img'); // no subscript, returns single element
Run Code Online (Sandbox Code Playgroud)
还是我错过了什么?
我有一系列带有列的行,我想选择一个input字段的值,该字段位于input字段(价格输入)的前一列中,我在释放键时调用函数.
我试过了:
quantity = $(this).parent().parent().children().val() ;
quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ;
Run Code Online (Sandbox Code Playgroud)
但是没有工作.
DOM的一个例子:
<div class="row">
<div class="column"><input class="inputQty" id="quantity0" /></div>
<div class="column"><input class="someOther" id="Other0" /></div>
<div class="column">
<div class="cSelect">
<select id="currency0"><option>£</option></select>
<input class="price" id="price0" />
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我正处于一个.each迭代的中间,想要为每个人调出第二个或第三个孩子.但是不能让它工作.
alert($(this + ' :nth-child(2)').attr('id'));
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一选择是像这样可怕的傻瓜:
$(this).children(':first').next().attr('id', 'ddParam' + newCount);
$(this).children(':first').next().next().attr('id', 'txt' + newCount);
$(this).children(':first').next().next().next().attr('id'...
Run Code Online (Sandbox Code Playgroud) 我可以为表添加许多行,但我无法删除很多行.我只能在每个顺序添加中删除1行.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#addCF").click(function(){
$("#customFields").append('<tr valign="top"><th scope="row"><label for="customFieldName">Custom Field</label></th><td><input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> <a href="javascript:void(0);" id="remCF">Remove</a></td></tr>');
$("#remCF").on('click',function(){
$(this).parent().parent().remove();
});
});
});
</script>
<table class="form-table" id="customFields">
<tr valign="top">
<th scope="row"><label for="customFieldName">Custom Field</label></th>
<td>
<input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" />
<input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" />
<a href="javascript:void(0);" id="addCF">Add</a>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
你可以在http://jsfiddle.net/3AJcj/看到代码
我需要帮助.
假设我的内容中有很多这样的内容div:<cite class="fn">blabla</cite>
如何用类检查每个cite标签的内容(在这种情况下:),看它是否等于"sometext"然后将它的颜色改为红色?blablafn
非常简单.
我在jQuery中使用prev()时遇到了麻烦,因为它没有选择正确的元素.
我的HTML结构如下:
<section id="about">
...
</section>
<hr>
<section id="contact">
...
</section>
Run Code Online (Sandbox Code Playgroud)
"活动"部分是#contact.我想选择上一节跳过<hr>
active = active.prev('section')似乎不起作用.我想我可能正在阅读错误的文档......
如果我拿出<hr>一切都很美妙.关于如何跳过<hr>on prev()的任何想法?
TIA
HTML:
<ul id="datalist">
</ul>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
function add(content){
ul=document.getElementsByTagName("ul");
var li=document.createElement("li");
li.innerHTML=content;
ul.appendChild(li);
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时add,Uncaught TypeError: Object #<NodeList> has no method 'appendChild'被抛出.知道为什么吗?
假设我有一个元素的层次结构,#root从根本上说.我可以$('#root > * > *')用来得到所有的孙子孙女.如果我已经拥有,我有什么方法可以做到的$('#root')吗?
$('#root').find('* > *')绝对不是它,因为它会从第一个明星的任何后代开心"开始",而不仅仅是#root孩子.
在jQuery中是否有任何函数可以"锚定"当前元素,从它的子元素开始?它已经困扰了我一段时间,我在文档中找不到类似的东西.
是否有人在DOM api中处理jQuery.closest()等价物?
看起来选择器级别2草案添加matches()等效于jQuery.is(),因此本机最接近应该更容易编写.添加closest()选择器了吗?
dom-traversal ×10
jquery ×8
javascript ×6
dom ×2
html-table ×1
standards ×1
tablerow ×1
w3c ×1