简体   繁体   English

jQuery:为什么:包含选择器返回undefined?

[英]jQuery: why does :contains selector return undefined?

I am testing a web application and using jQuery to locate a text containing specific string. 我正在测试一个Web应用程序并使用jQuery来查找包含特定字符串的文本。 This is the function I wrote, however, it doesn't seem to respond. 这是我写的功能,然而,它似乎没有回应。 I don't know what the tag of the element is ahead of the time. 我不知道该元素的标签是什么时候提前。

function get_element_by_text(text) {
    $(document).ready(function (){
        var selector = '*:contains(' + text + ')';
        return $(selector).get(0);
    });
} 

get_element_by_text("Unanswered"); returns undefined 返回undefined

The function get_element_by_text is not returning anything, you are having a dom ready handler inside it, the value is returned by that inner function. 函数get_element_by_text没有返回任何内容,你在其中有一个dom ready处理程序,该值由该内部函数返回。

So get_element_by_text does not return any value thus you are getting undefined as the result. 因此get_element_by_text不会返回任何值,因此您将得到未定义的结果。

Try 尝试

function get_element_by_text(text) {
    var selector = '*:contains(' + text + ')';
    return $(selector).get(0);
}
get_element_by_text("Unanswered");

but it will give html as the result because of your selector 但是由于你的选择器,它会给你带来html的结果

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

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