[英]JS: Selecting div's href's and applying the text of href to an input
我有一個名為#div1的div
,它獲得隨機值href
。 例如:
<div>
<a href="#"> Apple </a>
<a href="#"> Orange </a>
<a href="#"> Lemon </a>
<a href="#"> Banana </a>
</div>
怎么可能,當我點擊任何那些用Javascript
input
#input1時?
表格是: <input type="text" name="q" id="input1" >
非常感謝。
您的問題提示您希望href
屬性填充input
元素。
$('#div1 > a').click(function(event) {
event.preventDefault();
$('#input1').val($(this).attr('href'));
});
jsFiddle 。
如果您想通過href
屬性獲取整個路徑,請使用this.href
。
如果這是真的,你是后面的文本節點,更換this.href
以上$.trim($(this).text())
嘗試以下代碼在文本框中獲取檸檬,香蕉等值....
HTML:
<div1>
<a href="#"> Apple </a>
<a href="#"> Orange </a>
<a href="#"> Lemon </a>
<a href="#"> Banana </a>
</div>
<input type="text" name="q" id="input1" >
腳本:
<script type="text/javascript" >
$('div1 > a').click(function(event) {
event.preventDefault();
$('#input1').val($(this).html());
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.