简体   繁体   English

jQuery每个都不适用于某些类或一般的类?

[英]jQuery each doesn't work with certain classes, or classes in general?

I'm using Phonegap to build a small (test only) Macrumors application, and remote hosts actually work (there is no same host browser restrictions). 我正在使用Phonegap构建一个小型(仅测试)Macrumors应用程序,而远程主机实际上工作(没有相同的主机浏览器限制)。 I am using the jQuery Load() function to load the contents of the Macrumors homepage http://www.macrumors.com/ into a bin, hidden div, then the each function to loop through all the article classes to show the title in a box with a link to the page. 我正在使用jQuery Load()函数将Macrumors主页http://www.macrumors.com/的内容加载到bin,隐藏div中,然后每个函数循环遍历所有文章类以显示标题带有页面链接的框。

The problem is, after the Macrumors HTML content is loaded, the each function doesn't work with the article class. 问题是,在加载Macrumors HTML内容之后, each函数都不适用于文章类。 Also, in the load function (which allows you to specify certain selectors, id's and classes included, to only load in those sections of the page) the class doesn't work; 此外,在加载函数(允许您指定某些选择器,包含的id和类,仅加载页面的那些部分)中,该类不起作用; none of the classes do, in both the load function and each function. 在load函数和每个函数中都没有类。 And many Id's don't work in the each function either. 并且许多Id在each功能中each不起作用。

  • Can anybody explain this to a noob like me? 任何人都可以向像我这样的菜鸟解释这个吗?

Here is the code: 这是代码:

function onDeviceReady()
{
    // do your thing!
    $('#bin').load('http://www.macrumors.com/ #content');
    $('.article').each(function(){
    var title = $('a').html();
    $('#content').append('<b>'+title+'</b>')

    }); 
}

And the HTML stuff 和HTML的东西

    <body onload="onBodyLoad()">
       <div id="bin">
       </div>

       <div id="content">
       </div>

    </body>

I sincerely apologize if there's some very simple mistake here that I'm missing; 如果我遗漏了一些非常简单的错误,我真诚地道歉; I'm a major JS newbie. 我是一个主要的 JS新手。

.load() is asychronous. .load()是异步的。 It hasn't completed yet when you're executing .each() . 当你执行.each()时,它还没有完成。 You need to put your .each() and any other code that wants to operate on the results of the .load() in the success handler for .load() . 你需要把你的.each()和想要的结果操作的任何其他代码.load()在成功处理程序的.load()

You would do that like this: 你会这样做:

function onDeviceReady()
{
    // do your thing!
    $('#bin').load('http://www.macrumors.com/ #content', function() {
        $('.article').each(function(){
            var title = $('a').html();
            $('#content').append('<b>'+title+'</b>')
        }); 
    });
}

I'm also guessing that your .each() function isn't working quite right. 我也猜测你的.each()函数运行不正常。 If you want to get the link out of each .article object, you would need your code to be like this so that you're only finding the <a> tag in each .article object, not all <a> tags in the whole document: 如果你想从每个.article对象中获取链接,你需要你的代码是这样的,这样你才能在每个.article对象中找到<a>标签,而不是整个<a>标签文献:

function onDeviceReady()
{
    // do your thing!
    $('#bin').load('http://www.macrumors.com/ #content', function() {
        $('.article').each(function(){
            var title = $(this).find('a').html();
            $('#content').append('<b>'+title+'</b>')
        }); 
    });
}

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

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