简体   繁体   English

用jQuery解析不推荐使用的HTML

[英]Parsing horribly deprecated HTML with jQuery

I'm having some trouble understanding how to use nested selectors in jQuery. 我在理解如何在jQuery中使用嵌套选择器时遇到了一些麻烦。 I'm parsing a list of classes from my university and I want it to let me know if a class I want is open. 我正在解析我大学的课程列表,我希望它让我知道我想要的课程是否开放。 However, the website doesn't use css AT ALL, so the only way of identifying the class I want is open is to read the color attribute of the font tag. 但是,该网站完全不使用css AT,因此识别我想要的类的唯一方法是打开字体,即读取字体标签的color属性。

Here's the block of HTML I'm trying to read 这是我要阅读的HTML块

<TD><FONT FACE='Arial' SIZE='-1' COLOR='Black'>nameOfClass</TD>

Here's how am I'm trying to read it, and display an alert if the font tag attribute color of nameOfClass is "Black", which means its open. 这是我尝试阅读的方式,如果nameOfClass的字体标签属性颜色为“黑色”(表示黑色),则显示警告。 It's nasty but its the only way I can tell if the class is available or not. 这很讨厌,但它是我可以知道该类是否可用的唯一方法。

    function main() {

    $(document).ready(function(){
        if $("td").text()=="nameOfClass" 
            if $(this "font").attr("COLOR")=="Black" {
                alert("It actually works!");
            }
        });

I never get an alert when I run this though. 我运行此程序时从未收到警报。 I'm pretty sure its my syntax, it's been a long while since I did any sort of coding so I might be making some stupid mistake. 我很确定它是我的语法,因为我做了任何形式的编码已经很长时间了,所以我可能会犯一些愚蠢的错误。

You can use .children . 您可以使用.children But in order for your code to work, you have to iterate over all td elements, not just compare the text value of the first one: 但是为了使代码正常工作,您必须遍历所有td元素,而不仅仅是比较第一个元素的文本值:

$("td").each(function() {
    if($(this).text() === 'nameOfClass' && 
       $(this).children('font').attr('color') === 'Black') {
           alert("It actually works!");
    }
});

Otherwise, $("td").text()=="nameOfClass" only tests whether the text of the first td element in the page is "nameOfClass", which is certainly not what you want. 否则, $("td").text()=="nameOfClass"仅测试页面中第一个 td元素的文本是否为“ nameOfClass”,这当然不是您想要的。 You want to find all td element which contain that string. 您要查找包含该字符串的所有td元素。


You could do it much simpler if you'd directly select all font elements whose color attribute has the value "Black", with the attribute selector . 如果直接使用属性选择器选择color属性值为“ Black”的所有font元素,则可以使操作简单得多。 Then you filter out the ones that don't contain the class name and count how many elements are left over. 然后, 筛选出不包含类名的元素,并计算剩余的元素 If none, then the class is not open. 如果没有,则该类未打开。

var classIsOpen = $('font[color="Black"]').filter(function() { 
   return $(this).text() === 'nameOfClass';
}).length > 0;

You only need to do an exact comparison of the class name if it could occur as part of an other name, eg "Web" and "Advanced Web". 如果类名称可能作为其他名称的一部分出现,例如“ Web”和“ Advanced Web”,则只需对其进行精确比较。 If that's not the case, you can make the code even shorter, with the :contains selector: 如果不是这种情况,可以使用:contains选择器使代码更短:

var classIsOpen = $('font[color="Black"]:contains("nameOfClass")').length > 0;

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

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