内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
经典javascript:
var myvar = document.getElementById("abc"); abc.value += "test"; abc.value += "another test";
jQuery:
$("#abc").val($("#abc").val()+"test"); $("#abc").val($("#abc").val()+"another test");
有什么方法可以让jQuery更简洁,是否可以用一个隐藏的+=函数呢?我知道.val()不是一个属性,但我觉得一定有办法使这段代码更漂亮...
像这样的东西会很好:
$("#abc").valueAttribute += "test" $("#abc").val().content += "test" $("#abc").val().add("test")
你可以返回原始DOM元素。
$("#abc").get(0).value += "test";
否则,你必须写一个插件
$.fn.appendVal = function (newPart) { return this.each(function(){ $(this).val( $(this).val() + newPart); }); }; $("#abc").appendVal("test");