简体   繁体   English

如何使用jquery选择具有'abbr'属性特定值的元素

[英]How can I select element that has specific value for 'abbr' attribute with jquery

Part of my html is : 我的部分HTML是:

<tr id="row">
  <td abbr='selectme'>
    <div>Texti inside the div</div>
    Text outside the div
  </td>

  <td>
    any others I dont care about
  <td>
</tr>

And I am trying to select "Text outside the div" which is inside the td that has attribute 'selectme' . 我试图在具有'selectme'属性的td中选择“div之外的文本 How can I accomplish that with jquery? 我怎样才能用jquery来实现呢?

I tried like this: 我试过这样的:

$("#row").find("[abbr='selectme']").text() 

but it returns nothing. 但它什么也没有回报。

.text() returns the text content of the descendant elements also, filter them out .text()也返回后代元素的文本内容,将其过滤掉

var text = $("#row").find("[abbr='selectme']").contents().filter(function () {
    //select only text nodes of the targetted td element
    return this.nodeType == 3
}).text();

Demo: Fiddle 演示: 小提琴

The reason you are getting nothing in return is probably because you you have to wrap it in a 你得到任何回报的原因可能是因为你必须把它包装成一个

$( document ).ready(function() {}); or $(function(){});

Because div needs to be loaded in the DOM before you reach it through jquery otherwise you are accessing an element which is not loaded yet. 因为div需要在通过jquery到达之前加载到DOM中,否则您正在访问尚未加载的元素。 So by using this code: 所以通过使用此代码:

$(function () {
        alert($("#row").find("[abbr='selectme']").text());
});

you will get the following text: Texti inside the div Text outside the div 您将获得以下文本:div内的文本div中的文本

Now as you want to only get the text "text outside the div" which is plain text without wrapped in any tag you have to use .contetns().filter() function and to get where the 现在,因为你只想获得文本“div之外的文本”,这是纯文本而不包含在任何标记中,你必须使用.contetns()。filter()函数并获取

nodeType === 3

numeric value "3" is used to get the text nodes. 数值“3”用于获取文本节点。 so you final code will look like this (I am using alert for illustration) 所以你最终的代码看起来像这样(我使用警报说明)

 // Shorthand for $( document ).ready()
    $(function () {
        // getting the specific text and saving into a variable
        //getting the contents of the target element
        var myText = $("#row").find("[abbr='selectme']").contents()
        // using the filter to get the text node
            .filter(function () {
                return this.nodeType === 3;
            //getting the final text
            }).text();
        alert(myText);
    });

Now when you run the programme you will get the output "Text outside the div". 现在,当您运行程序时,您将获得输出“div之外的文本”。

Here is the working example http://jsfiddle.net/842nn/ 这是http://jsfiddle.net/842nn/的工作示例

您只需按attr名称搜索元素即可:

$("[abbr='selectme']").text();

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

相关问题 如何使用jQuery选择具有属性和属性不等于特定值的元素? - How to select element has attribute and attribute not equal to specific value using jquery? 如何选择仅具有特定类别的元素? - How can I select an element which has only a specific class? 如何在jQuery中的元素之后选择第一个特定的类 - How can I select the first specific class after element in jQuery 如何使用Jquery选择特定的嵌套元素 - How can I select specific nested element with Jquery 如何从元素中获取 jQuery 中数据属性的值? - How can I get the value of data attribute in jQuery from an element? 如何使用JQuery以编程方式翻转元素属性的值? - How can I programmatically flip the value of an element attribute using JQuery? 如何查找具有特定属性且具有特定值的元素 - How to find an element with a specific attribute that has a specific value 如何获取没有缩写的jquery flexigrid元素的文本? - How do I get the text for a jquery flexigrid element that does not have an abbr? 如何使用jquery将css属性添加到具有特定属性的元素? - How to add css property to element has specific attribute using jquery? 使用 jQuery,如何获得特定的下拉元素(<select></select> ) 基于下拉列表的值? - Using jQuery, how do I get a specific dropdown element (<select></select>) based on the value of the dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM