我有前任的标签。: <label id="labelId" class="control-label col-lg-4">Some text ()</label>, 我怎样才能在刹车中间插入文字,然后清除它?
我试过了 :
$('#labelId').replace("()", "(new)");
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用
Zak*_*rki 10
您必须使用text()替换来获取文本,replace()然后将替换后的文本设置回标签。
jQuery 解决方案:
var label_text = $('#labelId').text(); //Get the text
$('#labelId').text( label_text.replace("()", "(new)") ); //Replace and set the text back
Run Code Online (Sandbox Code Playgroud)
纯js解决方案:
var label_text = document.getElementById('labelId').innerText; //Get the text
document.getElementById('labelId').innerText = label_text.replace("()", "(new)"); //Replace and set the text back
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
var label_text = $('#labelId').text(); //Get the text
$('#labelId').text( label_text.replace("()", "(new)") ); //Replace and set the text back
Run Code Online (Sandbox Code Playgroud)
var label_text = document.getElementById('labelId').innerText; //Get the text
document.getElementById('labelId').innerText = label_text.replace("()", "(new)"); //Replace and set the text back
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8479 次 |
| 最近记录: |