简体   繁体   English

在javascript中通过id调用元素

[英]Call element by id in javascript

I have a repeater and have a label with an icon inside it. 我有一个转发器,里面有一个带有图标的标签。

 <strong><i id="iconProperties" class="icon-caret-right"></i>&nbsp;Properties</strong>

When i click a button the icon-caret-right must be turned to icon-caret-down. 当我点击一个按钮时,必须将icon-caret-right转为icon-caret-down。 i have written the code as follows: 我写的代码如下:

 $('#iconProperties').removeClass('icon-caret-right').addClass('icon-caret-down');

When i use this code only the first row of the repeater is working. 当我使用此代码时,只有转发器的第一行正在工作。 all other rows's icons are not changing. 所有其他行的图标都没有变化。 What is the mistake? 这是什么错误?

ASP.net generates unique ids for html elements and only one element can have id iconProperties that you used in selector. ASP.net为html元素生成唯一ids ,并且只有一个元素可以具有您在选择器中使用的id iconProperties You can use Attribute Contains Selector [name*="value"] instead of id selector to get all the matching elements. 您可以使用Attribute Contains Selector [name * =“value”]代替id选择器来获取所有匹配元素。

$('[id*=iconProperties]').removeClass('icon-caret-right').addClass('icon-caret-down');

If your ids have a similar name, you're probably after something like $('[id^=iconProperties]').removeClass('icon-caret-right').addClass('icon-caret-down'); 如果你的ids有一个类似的名字,你可能会像$('[id^=iconProperties]').removeClass('icon-caret-right').addClass('icon-caret-down');
Which will update all items beginning with id "iconProperties". 这将更新以id“iconProperties”开头的所有项目。

It might be worth noting that it is common practice to use unique ids . 值得注意的是,通常的做法是使用唯一的ids

尝试这个:

$('#iconProperties').find('.icon-caret-right').replaceWith('.icon-caret-down');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM