我试图将链接onClick的值(显示的名称)复制到文本框中。有可能吗?
以下是我的草率尝试 -
<div class="service_list">
<ul style="list-style-type:square; display:none;" id="ullist">
<li><a href="#" class="service1">Service 1</a>
</li>
<li><a href="#" class="service2">Service 2</a>
</li>
</ul>
</div>
<input type="text" id="textbox" />
<br />
<table>
<tr>
<td>
<div class="button"> <a href="#" download="servicename.txt" class="button" id="button">Download</a>
</div>
</td>
<td>
<div class="list"> <a href="#" class="list" id="list">List of Services</a>
</div>
</td>
</tr>
</table>
这是JS -
var link_selected = document.querySelector('.service_list'); //edited
var input_field = document.getElementById("input");
link_selected.onclick = function (e) {
var selected = document.querySelector('.service_list').value; //edited
input_field.value = selected;
};
这里是相同的JSFiddle: https://jsfiddle.net/aishwarya26/rebpb5h2/
答案 0 :(得分:3)
您可以为var text = document.getElementById('textbox');
var anchor = document.querySelector('#button');
var links = document.querySelectorAll('#ullist a');
for(var i = 0;i< links.length;i++){
links[i].onclick = function(){
text.value = this.textContent || this.innerText
}
}
元素中的每个链接添加点击处理程序,然后设置文本框的值
jQuery(function($){
var $text = $('#textbox');
$('#ullist a').click(function () {
$text.val($(this).text());
});
$('#button').click(function (e) {
var textbox_text = $text.val();
var textbox_file = new Blob([textbox_text], {
"type": "application/bat"
});
this.href = URL.createObjectURL(textbox_file);
this.download = "servicename.txt";
});
$('#list').click(function (e) {
$('#ullist').toggle();
});
})
演示:Fiddle
使用jQuery
TextView
演示:Fiddle
答案 1 :(得分:2)
当JQuery被标记时,可以使用JQuery完成!
$(document).on('click', 'a', function(e) {
e.preventDefault();
//e = $(this).attr('href');//this gets the href
e = $(this).text();//This gets the text
$('input').val(e);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input readonly>
<a href="http://google.com">Google<a>
<a href="http://booble.com">Booble<a>
<a href="http://doodle.com">Doodle<a>
<a href="http://coocle.com">Coocle<a>
<a href="http://roorle.com">Roorle<a>
&#13;
答案 2 :(得分:1)
像这样在页面加载上使用单击触发器,如果你愿意涉足一些jquery:
$('a').click(function(){
$('#textbox').val($(this).html());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="service_list">
<ul style="list-style-type:square; display:none;" id="ullist">
<li><a href="#" class="service1">Service 1</a>
</li>
<li><a href="#" class="service2">Service 2</a>
</li>
</ul>
</div>
<input type="text" id="textbox" />
<br />
<table>
<tr>
<td>
<div class="button"> <a href="#" download="servicename.txt" class="button" id="button">Download</a>
</div>
</td>
<td>
<div class="list"> <a href="#" class="list" id="list">List of Services</a>
</div>
</td>
</tr>
</table>
答案 3 :(得分:1)
你也可以使用实时jquery点击这个
<div id="video-player">
<video width="100%" height="100%" loop="" autoplay="">
<source type="video/webm" src="Video/eat.webm"></source>
</video>
</div>