是否可以做“.value + =”在JQuery? [英] Is it possible to do ".value +=" in JQuery?

查看:98
本文介绍了是否可以做“.value + =”在JQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经典javascript:

Classic 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()不是属性,但我觉得必须有办法让这段代码看起来更漂亮......

Is there a way to make my Jquery prettier, maybe with a hidden += function that I could use? I know that .val() is not an attribute, but I feel there must be a way to make this code more beautiful to look at...

这样的东西会很棒:

 $("#abc").valueAttribute += "test"
 $("#abc").val().content += "test"
 $("#abc").val().add("test")


推荐答案

从jQuery 1.4开始,可以将函数传递给 .val() 获取当前值作为第二个参数:

Since jQuery 1.4, it is possible to pass a function to .val() which gets the current value as second argument:

$("#abc").val(function(i, val) {
    return val + "test";
});

这篇关于是否可以做“.value + =”在JQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆