简体   繁体   English

如何使用jquery获取下一个div的内部元素的文本?

[英]How to get the text of the inside element of the next div using jquery?

I have two divs... Then when I clicked the 1st div, i want to get the text inside of h3 of the 2nd div Here is my codes... But none of the jquery code is working.. any help to fix my code, please... thanks我有两个 div ......然后当我点击第一个 div 时,我想在第二个 divh3中获取文本这是我的代码......但是没有一个 jquery 代码正在工作......任何帮助修复我代码,请...谢谢

HTML HTML

<div class="timeline-badge" style="background-image:url(images/couple-9.jpg);">CLICK ME</div>
<div class="timeline-panel">
    qqqq
    <div class="timeline-heading">
        wwww
        <h3 class="timeline-title">Hello</h3>
        <span class="date"><i class="icon-calendar"></i>February 00, 2017</span>
    </div>
    <div class="timeline-body">
        <p>III</p>
    </div>
</div>

JQUERY / JAVASCRIPT JQUERY / JAVASCRIPT

$('.timeline-badge').on('click', function() {
    alert($(this).closest(".timeline-panel + div + h3").text());
    alert($( '.timeline-badge > div:eq(0) + h3:eq(0)' ).children().text());
    alert($(this).nextAll( '.timeline-heading').eq(0).innerhtml);
});

http://jsfiddle.net/rK4qc/119/ http://jsfiddle.net/rK4qc/119/

The .timeline-panel is next sibling of .timeline-badge . .timeline-panel.timeline-badge下一个兄弟。 So you should use .next() to selecting it.所以你应该使用.next()来选择它。 Then use .find() to finding .timeline-title in it.然后使用.find()在其中查找.timeline-title

$('.timeline-badge').on('click', function() {
  var text = $(this).next().find(".timeline-title").text();
  console.log(text);
});

 $('.timeline-badge').on('click', function() { var text = $(this).next().find(".timeline-title").text(); console.log(text); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="timeline-badge" style="background-image:url(images/couple-9.jpg);">CLICK ME</div> <div class="timeline-panel"> qqqq <div class="timeline-heading"> wwww <h3 class="timeline-title">Hello</h3> <span class="date"><i class="icon-calendar"></i>February 00, 2017</span> </div> <div class="timeline-body"> <p>III</p> </div> </div>

Use Jquery's siblings selectors https://api.jquery.com/siblings/使用 Jquery 的兄弟选择器https://api.jquery.com/siblings/

$('.timeline-badge').on('click', function() {
    console.log($(this).siblings());
});

Go get it by the class name of h3通过h3的类名去获取

$('.timeline-badge').on('click', function() {
    alert($(this).next().find('h3.timeline-title').text());
});

您可以通过以下方式获取 h3 标签的值:

$('div.timeline-panel div.timeline-heading h3.timeline-title').val();

use this code.使用此代码。

$(document).on("click",".timeline-badge",function() {
   alert("H3 IS " + $('h3.timeline-title').text());
});

and check this fiddle for the code.并检查这个小提琴的代码。 http://jsfiddle.net/rK4qc/121/ http://jsfiddle.net/rK4qc/121/

For the second div just add div for it and get the text user the .text() function.对于第二个 div,只需为其添加 div 并让文本用户使用 .text() 函数。

this will work for you:-这对你有用:-

$('.timeline-badge').on('click', function() {

alert($(this).next().find('h3.timeline-title').text());
});

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

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