如何从选定的Li中获取特定的div文本

时间:2019-05-16 12:27:07

标签: jquery html

我创建了多个ul,我希望从特定的li中获得特定的div值

我试图为此创建一个函数,但是它不起作用

$(".answer-list li").click(function() {
	$(this).addClass('active');
	BusinessLogic();
});

function BusinessLogic(){
   var selText = $('.answer-list li .active div p').text();
   alert(selText);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<ul class="answer-list" id="answer_list1">
		<li>
			<a tabindex="1" id="question1">
				<span class="letter-option">A.</span>
				<div>
					<p>Plan risk management</p>
				</div>
			</a>
		</li>
		<li class="active">
			<a tabindex="2" id="question1">
				<span class="letter-option">B.</span>
				<div>
					<p>Identify risks</p>
				</div>
			</a>
		</li>
		<li>
			<a tabindex="3" id="question1">
				<span class="letter-option">C.</span>
				<div>
					<p>Plan risk responses</p>
				</div>
			</a>
		
		</li><li>
			<a tabindex="4" id="question1">
				<span class="letter-option">D.</span>
				<div>
					<p>Perform qualitative risk analysis</p>
				</div>
			</a>
		</li>
</ul>

我想选择li div值,但显示空白

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

像这样尝试获取div标签的innerhtml。

$(".answer-list li").click(function() {
	$('li').removeClass('active');
  $(this).addClass('active');
	BusinessLogic();
});

function BusinessLogic(){
   var selText = $('.answer-list li.active div p').text();
   console.log(selText);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<ul class="answer-list" id="answer_list1">
        <li>
            <a tabindex="1" id="question1">
                <span class="letter-option">A.</span>
                <div>
                    <p>Plan risk management</p>
                </div>
            </a>
        </li>
        <li class="active">
            <a tabindex="2" id="question1">
                <span class="letter-option">B.</span>
                <div>
                    <p>Identify risks</p>
                </div>
            </a>
        </li>
        <li>
            <a tabindex="3" id="question1">
                <span class="letter-option">C.</span>
                <div>
                    <p>Plan risk responses</p>
                </div>
            </a>

        </li><li>
            <a tabindex="4" id="question1">
                <span class="letter-option">D.</span>
                <div>
                    <p>Perform qualitative risk analysis</p>
                </div>
            </a>
        </li>
</ul>

答案 1 :(得分:1)

删除li url(r'^account/(?P<account_id>\w+)/myviews',MyModelViewset.as_view({'get':'list'})) 事件上的有效Class并删除空间click

.answer-list li.active

$(".answer-list li").click(function() {
      $('.answer-list li').removeClass('active');
        $(this).addClass('active');
        BusinessLogic();
    });

    function BusinessLogic(){
       var selText = $('.answer-list li.active div p').text();
       alert(selText);
    }
$(".answer-list li").click(function() {
  $('.answer-list li').removeClass('active');
	$(this).addClass('active');
	BusinessLogic();
});

function BusinessLogic(){
   var selText = $('.answer-list li.active div p').text();
   alert(selText);
}