繁体   English   中英

从dom元素ID中删除字符串

[英]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());

JSFIDDLE

像这样使用:

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.

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