从jquery

时间:2017-02-17 20:48:11

标签: javascript jquery

目前我有类似的东西

  <ul class="patient-class" id="patientSelection">
            <li>          
                <label for="plist">        
                    <div class="patient-details"><span class="pheading">Foo</span><span class="page"><a href="SomeLink">14 years</a></span>
                    </div>
                </label>
            </li>
  </ul>

现在我有一个jquery函数,告诉我单击了哪个li项目

<script>
        $("#patientSelection li").click(function() {
          alert('Clicked list.'+$(this).value);
         });
</script>

现在我的问题是如何提醒14年的链接。这是SomeLink。基本上我只想捕获链接文本?

2 个答案:

答案 0 :(得分:3)

您可以使用.find()获取锚元素,您可以获得.text()

var clicks = 0; //counts how may picks have been made in each turn
var firstchoice; //stores index of first card selected
var secondchoice; //stores index of second card selected
var match = 0; //counts matches made
var backcard = "deck.jpg"; //shows back of card when turned over

var faces = []; //array to store card images
faces[0] = 'pic1.jpg';
faces[1] = 'pic2.jpg';
faces[2] = 'pic3.jpg';
faces[3] = 'pic3.jpg';
faces[4] = 'pic2.jpg';
faces[5] = 'pic1.jpg';

function choose(card) {
        if (clicks === 2) {
            return;
        }
        if (clicks === 0) {
            firstchoice = card;
            document.images[card].src = faces[card];
            clicks = 1;
        } else {
            clicks = 2;
            secondchoice = card;
            document.images[card].src = faces[card];
            timer = setInterval("check()", 1000);
        }
    }
    /* Check to see if a match is made */

function check() {
    clearInterval(timer); //stop timer
    if (faces[secondchoice] === faces[firstchoice]) {
        match++;
        document.getElementById("matches").innerHTML = match;
    } else {
        document.images[firstchoice].src = backcard;
        document.images[secondchoice].src = backcard;
        clicks = 0;
        return;
    }
}
$("#patientSelection li").click(function() {
  console.log($(this).find('.page a').text());
});

答案 1 :(得分:2)

使用.find()查找被点击的li's元素的后代:

$(this).find( 'a' ).text();