我正在使用jQuery AJAX检索/获取内容,将其显示在页面上,并尝试从以下具有3个类名的范围获取文本:

<span class="price eur priceData">198,91 €</span>

我怎样才能从那个范围得到“198,91”这个数字?
我正在使用以下代码检索文本(这不起作用):
$("span.price.eur.priceData").text();

您请求了更多代码,这里是:
$(document).ready(function() {
                Read();
            });

            function Read() {
                $.ajax({
                    url: "index.php?action=getData",
                    cache: false,
                    success: function(html){
                        if(html == "BAD") {
                            $("#my_results").empty().append("Failed!");
                        } else {
                            $("#my_results").empty().append("Successful!");
                            $("#page_content").empty().append('<xmp>'+html+'</xmp>');
                            var text=$("span.price.eur.priceData").text();
                            alert(text);
                        }
                    }
                });
            }

我甚至尝试过使用SetTimeout()和Delay(),但仍然没有成功。
解决方案:(多亏了影子向导)
$(html).find("span.price.eur.priceData").text();

最佳答案

您的代码失败,因为您用<xmp>标记包装HTML内容,将它们呈现为纯文本,因此jQuery在其中找不到任何元素。
您可以这样使用原始内容:

$("#my_results").empty().append("Successful!");
$("#page_content").empty().append('<xmp>'+html+'</xmp>');
var text = $(html).find("span.price.eur.priceData").text();
alert(text);

关于jquery - jQuery-无法从具有3个类名的范围中获取文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9680730/

10-17 00:51