简体   繁体   English

如何使用jQuery获取单击元素的html内容

[英]How to get the html content of a clicked element using jQuery

I am trying to get the inner html content of a particularly clicked div, I have a number of div elements. 我试图获得特别点击的div的内部html内容,我有很多div元素。

Look at the code I use below. 看下面我使用的代码。

 $(function() { $( "#try" ).click(function() { var clickedValue = $(this).find('div').text(); alert(clickedValue); }); }); 
 <div id="try"> <div class="cchoice" style="background-color: blue;">B</div> <div class="cchoice" style="background-color: red;">R</div> <div class="cchoice" style="background-color: yellow;">Y</div> <div class="cchoice" style="background-color: black;">B</div> <div class="cchoice" style="background-color: gray;">G</div> <div class="cchoice" style="background-color: white;">W</div> </div> 

I want to do it in a way such that B appears when the div with background color of blue is clicked on but then it seems something is wrong with the way I am doing it. 我想以这样的方式进行操作,即当单击具有蓝色背景色的div时出现B,但随后看来我的操作方式有问题。 For some reasons, I cannot give id attribute to those set of div and they must have the same class. 由于某些原因,我无法将id属性赋予这些div集,并且它们必须具有相同的类。

The problem with the code above is that when I click on any of the 6 div elements, the clickedvalue will be BRYBGW. 上面代码的问题是,当我单击6个div元素中的任何一个时,clickedvalue将为BRYBGW。

$(this).find('div') will return all the div elements within the #try element, instead you want to target only the clicked div $(this).find('div')将返回#try元素内的所有div元素,而您只想定位单击的div

You can use event.target 您可以使用event.target

 $(function() { $("#try").click(function(e) { var clickedValue = $(e.target).text(); alert(clickedValue); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="try"> <div class="cchoice" style="background-color: blue;">B</div> <div class="cchoice" style="background-color: red;">R</div> <div class="cchoice" style="background-color: yellow;">Y</div> <div class="cchoice" style="background-color: black;">B</div> <div class="cchoice" style="background-color: gray;">G</div> <div class="cchoice" style="background-color: white;">W</div> </div> 


But I would recommend targeting the actual child element using the click handler.... either by directly binding the handler to them or by using event delegation 但我建议您使用点击处理程序来定位实际的子元素。...通过将处理程序直接绑定到它们或使用事件委托

 $(function() { //or $("#try > div").click(function(e) { $("#try").on('click', 'div', function(e) { var clickedValue = $(this).html(); alert(clickedValue); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="try"> <div class="cchoice" style="background-color: blue;">B</div> <div class="cchoice" style="background-color: red;">R</div> <div class="cchoice" style="background-color: yellow;">Y</div> <div class="cchoice" style="background-color: black;">B</div> <div class="cchoice" style="background-color: gray;">G</div> <div class="cchoice" style="background-color: white;">W</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM