jQuery获取所选div的多个锚文本

时间:2018-11-26 23:24:28

标签: jquery

我有一个列表,其中包含保存多个锚点的div。当我选择div时,我想获取所有锚文本以结转到新的div。有什么想法吗?

	$('.mwCont li').on('click', function(event){
		var mweek = $(this).find('ul > li:nth-child(1) > a').text;
		var mwdate = $(this).find('ul > li:nth-child(2) > a').text;
		$('#newdiv1').text(mweek);
		$('#newdiv2').text(mwdate);
	});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="mwBar">
		<section>
			<div class="mwbutton" id="showTop">
				<div class="newdiv1" id="newdiv1">MW 13</div>
				<div class="newdiv2" id="newdiv2">20 Nov - 23 Nov</div>
			</div>
		</section>
	</div>

    <div class="mwCont">
		<div class = "b1">
		 	<ul>
			   <li>
					<div class = "mwdiv">
						 <a>MW 5</a>
						 <a>26 Dec - 28 Jan</a>
						</div>
				</li>
			</ul>
		</div>
	</div>

1 个答案:

答案 0 :(得分:1)

问题似乎出在用于查找链接的选择器中,并且文本未正确调用(应该是text(),因此它会执行):

$('.mwCont li').on('click', function(event){
    var links = $(this).find('a');
    var mweek = links.eq(0).text();// First link text
    var mwdate = links.eq(1).text();// Second link text
    $('#newdiv1').text(mweek);
    $('#newdiv2').text(mwdate);
});