[英]Remove of string from dom element id
我需要从dom中的元素ID中删除一个字符串(“ DTE_Field_”)。
<select id="DTE_Field_CD_PAIS" dependent-group="PAIS" dependent-group-level="1" class="form-control"></select>
var str=$(el).attr("id");
str.replace("DTE_Field_","");
将attr()
方法与callback一起使用 。 这将遍历元素,您可以获取旧的属性值作为回调参数,可以通过使用String#replace
方法返回DTE_FIELD
字符串来更新属性。
$('select').attr('id', function(i, oldId) {
return oldId.replace('DTE_field_', '');
});
$('select').attr('id', function(i, oldId) { return oldId.replace('DTE_Field_', ''); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="DTE_Field_CD_PAIS" dependent-group="PAIS" dependent-group-level="1" class="form-control"></select>
如果不总是选择元素,则可以使用attribute contains selector
(或者如果DTE_field_
始终位于开头,则使用attribute starts with selector
)。
$('[id*="DTE_field_"]').attr('id', function(i, oldId) {
return oldId.replace('DTE_field_', '');
});
您可以拆分string( id
)并使用pop()
方法获取数组的最后一个元素
var str=$('#DTE_Field_CD_PAIS').attr("id");
alert(str.split("DTE_Field_").pop());
像这样使用:
var string=$('#DTE_Field_CD_PAIS').attr("id");
console.log(str.split("DTE_Field_").pop());
$('[id^="DTE_FIELD_"]').each(function () {
$(this).attr('id', $(this).attr('id').replace('DTE_FIELD_',''));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.