通过锚点获取段落文本和值(Jquery)

时间:2014-10-30 05:39:48

标签: jquery

我需要获得段落价值&单击锚标记上的文字

<div class="tags-group inner-box">
    <p value="1">abc university <a href="#" class="cross"></a></p>
    <p value="2">xyz university <a href="#" class="cross"></a></p>
</div>

3 个答案:

答案 0 :(得分:1)

你可以

//dom ready handler
jQuery(function($) {
  //click handler for the anchor element
  $('.tags-group .cross').click(function() {
    //find the `p` element of the anchor
    var $p = $(this).parent(),
      //read the p elements value attribute
      value = $p.attr('value'),
      //read the `p` element's text
      text = $p.text();
    alert(value + ':' + text)
  })
})
a.cross {
  display: inline-blick;
  line-height: 20px;
  padding: 0 10px;
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="tags-group inner-box">
  <p value="1">abc university
    <a href="#" class="cross"></a>
  </p>
  <p value="2">xyz university
    <a href="#" class="cross"></a>
  </p>
</div>

答案 1 :(得分:0)

$(".inner-box .cross").click(function()
{
  var paragrapthValue = $(this).parent().val();
}

答案 2 :(得分:0)

以下是一个示例:http://jsfiddle.net/davidzapata/hqnur3k6/

$('.cross').click(function(evt){
    evt.preventDefault();
    var theP = $(this).closest('p');
    console.log('The P value:', theP.attr('value'));
    console.log('The P text:', theP.text());
});