<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
</head>
<style type="text/css">
.box{width: 600px;height: 300px;border: 1px solid #ccc;margin: 0 auto}
.func span{margin-right: 10px;cursor: pointer;}
</style>
<body>
<p class="box">
<input type="text" name="" value="">
</p>
<p class="func">
<span>补水,</span><span>保湿,</span><span>去皱,</span><span>美白,</span>
</p>
</body>
<script type="text/javascript">
$(".func span").click(function(){
h=$(this).html();
$(".box input").attr("value").append(h);
});
</script>
</html>
What I wrote here is wrong. I want to click span and then add a value to the input value
$(".box input").val($(".box input").val() + h)
I don’t know what you mean by appending? What I understand is that originally
val
was an empty string'', I clicked on 'Hydration,', and then the strings were concatenated,val
became 'Hydrating,', then I clicked on 'Moisturizing,' and appendedvar
It becomes 'hydrating, moisturizing,'Everything mentioned above is correct. You may not understand the difference between append and direct assignment to val(). Append inserts an element into an existing dom node, such as: