本文介绍了在输入字段后更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不丢失输入字段的情况下将"2年订阅"更改为"2年:"
How do I change the "2 Year Subscription" to "2 year:" without losing the input field
<div class="radioOption subscription1Col3">
<input class="buyNow" type="radio" name="product2" value="6_0" checked="">2 Year Subscription</div>
推荐答案
这是相对简单的,我建议以下内容(尽管目前未经测试):
This is relatively simple, and I'd suggest the following (though currently untested):
var input = $('.radioOption.subscription1Col3 input'),
text = input[0].nextSibling.nodeValue,
newText = text.replace(/ subscription/i,': ');
input[0].nextSibling.nodeValue = newText;
或者可能更希望使用此版本,因为它将调整后的文本保留在label
元素中,这有助于UI并使得将来更容易定位该文本内容:
Or possibly this version might be preferred, since it leaves the adjusted text wrapped in a label
element, which aids UI and makes it easier to target that text content in future:
var input = $('#inputID'),
label = $('<label />',{'for' : 'inputID'})
.text(input[0].nextSibling.nodeValue.replace(/ Subscription/i,': '));
input.parent().empty().append(input, label);
但是,这确实要求input
具有id
属性,以便label
在语义上附加到该input
.
This does, though, require the input
to have an id
attribute so that the label
is semantically attached to that input
.
这篇关于在输入字段后更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!