<!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
扔个三星炸死你2017-07-05 11:04:29
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 appended var
It becomes 'hydrating, moisturizing,'
<!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 id= "id_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).text();
$("#id_box_input").val($("#id_box_input").val + h);
});
</script>
</html>
怪我咯2017-07-05 11:04:29
$(".func span").click(function(){
var text = $(this).text();
var $input = $(".box input")
$input.val($input.val()+text);
});
typecho2017-07-05 11:04:29
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:
1. 把<span>补水,</span> 插入到p中
var span = '<span>补水,</span>';
$("p").append(span),此时span就被加入到p中了
2. 而赋值直接是在input中给定一个值
直接
$("input").val(你需要插入的值)
可以试试看
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>时间</title>
<style type="text/css">
.box{width: 600px;height: 300px;border: 1px solid #ccc;margin: 0 auto}
.func span{margin-right: 10px;cursor: pointer;}
</style>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
<body>
<p class="box" id="p">
<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").val($(".box input").val() + h); // val
$("#p").append(h); // append
});
</script>
</html>