访问触发事件的元素中的文本 [英] Accessing the text in an element that triggered the event
本文介绍了访问触发事件的元素中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个无序列表,并且我想在单击列表项时将按钮上的文本更改为列表项中的文本.到目前为止,我有:
I have an unordered list and I want to change the text on a button when a list item is clicked to the text in the list item. So far, I have:
$(document).ready(function() {
$('.List-item').on("click",function(){
$('#selectedOption').text("hi");
});
});
当单击任何列表项时,这会将按钮文本更改为hi
.如何获得显示列表项文本的按钮?我需要将列表项传递到函数中吗?
This will change the button text to hi
when any list item is clicked. How can I get the button to display the list item text? Do I need to pass the list item into the function?
推荐答案
尝试:
$(document).ready(function() {
$('.List-item').on("click",function(){
$('#selectedOption').text($(this).text());
});
});
因为这将涉及您单击的内容. 另外,您可以使用
Because this will refer to what you click. Also, you can use
event.target.innerText
在没有jQuery的情况下获取点击的li文本
to get the clicked li text without jQuery
这篇关于访问触发事件的元素中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文