<input type="button" id="convert" value="Convert"/>
<input type="button" id="convert-back" value="Convert Back"/>
#convert 将转换所有具有lang=en 属性的输入文件中的文本。 #convert-back 按钮将执行相反的操作。让我知道它是否适合你
$(document).ready(function () {
$("#convert").click(function () {
$("input:lang(en)").each(function () {
var textArray = $(this).val();
var replaceText = textArray.replace("Hello", "Hi").replace("Good afternoon!", "Hi!");
$(this).val(replaceText)
});
});
$("#convert-back").click(function () {
$("input:lang(en)").each(function () {
var textArray = $(this).val();
var replaceText = textArray.replace("Hi", "Hello").replace("Hi!", "Good afternoon!");
$(this).val(replaceText)
});
});
});
编辑
HTML 部分
<input type="text" lang="en" value="Hello" alt="Hello, stack"/>
<input type="text" lang="en" value="Hello" alt="Hello, over"/>
<input type="text" lang="en" value="Hello" alt="Hello, flow"/>
<input type="text" lang="en" value="Hello" alt="Hello, stack"/>
<input type="text" lang="en" value="Hello" alt="Hello, over"/>
<input type="button" id="convert" value="Convert"/>
jQuery部分
$(document).ready(function () {
$("#convert").click(function () {
$("input:lang(en)").each(function () {
var textArray = $(this).val();
var newArray = $(this).attr("alt");
$(this).val(newArray);
$(this).attr("alt", textArray);
});
if ($(this).val() == "Convert") { $(this).val("Convert back"); }
else { $(this).val("Convert"); }
});
});
将旧值存储到输入框的alt 标记中以再次替换。如果对您有帮助,请告诉我。