Get one of p tag value in td when tap on td tag

时间:2016-02-03 04:13:44

标签: javascript jquery html

Please help me.

I want to detect the tapping on td. But I also need to get the one of the p tag (cal_date) value when tapped. How can I get the cal_date value?

JQUERY CURRENT CODE:

$( document ).on ( "tap", "td", function(event) { 
    //code here
    // i want to get the cal_date value in this block
});

I can't not use this way, because it will only can detect tap on the p.cal_date tag, not on the td tag...

$( document ).on ( "tap", ".cal_date", function(event) {

});

HTML:

<td><p class="cal_date" id="">7</p><br><p class="cal_rooms">16</p></td>

2 个答案:

答案 0 :(得分:0)

Find the .cal_date element inside the tapped TD, and get it's text

$(document).on ("tap", "td", function(event) { 

    var value = $(this).find('.cal_date').text();

});

答案 1 :(得分:0)

Inside the tap handler this refers to the tapped tdelement, so you can use .find() to get the cal_date element within it

$(document).on("tap", "td", function(event) {
  //code here
  // i want to get the cal_date value in this block
  var cal_date = $(this).find('.cal_date').text()
});