jQuery:为什么:contains选择器返回未定义? [英] jQuery: why does :contains selector return undefined?

查看:99
本文介绍了jQuery:为什么:contains选择器返回未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个Web应用程序,并使用jQuery查找包含特定字符串的文本.这是我编写的函数,但是似乎没有响应.我不知道该元素的标签是什么时间.

I am testing a web application and using jQuery to locate a text containing specific string. 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");返回未定义

推荐答案

函数get_element_by_text不返回任何内容,您内部具有dom ready处理程序,该值由该内部函数返回.

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不返回任何值,因此您将得到未定义的结果.

So get_element_by_text does not return any value thus you are getting undefined as the result.

尝试

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

但由于选择器的原因,它将给出html作为结果

but it will give html as the result because of your selector

这篇关于jQuery:为什么:contains选择器返回未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆