使用 JS 访问 HTML 对象内部的对象

发布于 2024-12-11 07:14:06 字数 573 浏览 1 评论 0原文

我正在玩家表 div 内创建一个 div 标签数组。我正在获取所有带有 .players 类的 div 标签。类名为 .players 的 div 内部有输入字段和链接字段。我希望能够操纵这些(删除、添加类等...)

我认为有效的方法是这样的:

$(divarray[j]+' .link').hide();
$(divarray[j]+' a').remove('.link');

但它不起作用。有什么想法吗?我确信这很简单,但这是我第一次接触 JS :)

var divarray = $('#player-table > .players');
        for( var j = 0; j < 10; j++){
        $(divarray[j]).removeClass("players");
        $(divarray[j]).addClass("selected_players");
        $('#debug').append(divarray[j]);
        $(divarray[j]+' a').hide();
    }

I am creating an array of div tags inside the player table div. I'm getting all div tags with class .players. The divs with class name .players have input fieds and a link field inside. I want to be able to manipulate these (remove, add class, etc...)

What I thought would work would be something like:

$(divarray[j]+' .link').hide();
$(divarray[j]+' a').remove('.link');

But it's not working. Any thoughts? I'm sure it's something simple but it's my first time at JS :)

var divarray = $('#player-table > .players');
        for( var j = 0; j < 10; j++){
        $(divarray[j]).removeClass("players");
        $(divarray[j]).addClass("selected_players");
        $('#debug').append(divarray[j]);
        $(divarray[j]+' a').hide();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-12-18 07:14:06

首先,您不能仅将 jQuery 对象或 DOM 节点与字符串连接起来来创建新的选择器。 jQuery 为这种情况提供了方法,在这种情况下,您已经有一个对象或 DOM 节点,并且想要查找其他相关节点。

其次,使用 jQuery 有更好的方法来处理一组元素。这是您的代码,以更像 jQuery 的方式。这只是一个例子,因为我不知道 HTML 结构。您必须对其进行调整,以便它选择并应用到正确的元素。

$('#player-table > .players').slice(0,10) // gets the first 10 elements
 .removeClass("players")                  // removes the class from all of them
 .addClass("selected_players")            // adds the class
 .find('a').hide().end()                  // finds all descendant links and hides them
 .appendTo('#debug');                     // appends all elements to `#debug`

正如您可能看到的,最后一行只有一个分号。这意味着整个代码块只是一个语句,但将其分成几行可以提高可读性。

它之所以有效,是因为 流畅的界面,这是 jQuery 大力打造的一个概念使用.它可以让您避免一遍又一遍地创建 jQuery 对象,就像 ($(divarray[j])) 一样。

另一个优点是您可以同时处理整个元素集,而不必像使用“正常”DOM 操作方法那样显式地迭代每个元素。

对于学习 JavaScript,我推荐 MDN JavaScript 指南
jQuery 有一些教程 和一个非常好的API 文档

仔细阅读它们以了解基础知识。您不能期望在不先阅读其说明的情况下能够使用该工具。

First of all, you cannot just concatenate jQuery objects or DOM nodes with strings to create new selectors. jQuery provides methods for this kind of situations, where you already have an object or DOM node and want to find other related nodes.

Second, with jQuery there are much better ways to process a set of elements. Here is your code in more jQuery-like way. This is just an example, because I don't know the HTML structure. You have to adjust it so that it selects and applies to the correct elements.

$('#player-table > .players').slice(0,10) // gets the first 10 elements
 .removeClass("players")                  // removes the class from all of them
 .addClass("selected_players")            // adds the class
 .find('a').hide().end()                  // finds all descendant links and hides them
 .appendTo('#debug');                     // appends all elements to `#debug`

As you maybe see, there is only one semicolon at the last line. That means this whole code block is just one statement, but splitting it up over several lines increases readability.

It works because of the fluent interface, a concept which jQuery heavily makes use of. It lets you avoid creating jQuery objects over and over again, like you do ($(divarray[j])).

Another advantage is that you can work on the whole set of elements at once and don't have to iterate over every element explicitly like you have to do with "normal" DOM manipulation methods.

For learning JavaScript, I recommend the MDN JavaScript Guide.
jQuery has a couple of tutorials and a very good API documentation.

Read them thoroughly to understand the basics. You cannot expect to be able to use a tool without reading its instructions first.

郁金香雨 2024-12-18 07:14:06

尝试这个说明

$(divarray[j]).find('.link').hide();
$(divarray[j]).find('a').remove('.link');

也尝试一下

$(divarray[j]).find('.link:first').hide();  

如果您只需要处理第一个元素

希望它有帮助

Try this istructions

$(divarray[j]).find('.link').hide();
$(divarray[j]).find('a').remove('.link');

Try also

$(divarray[j]).find('.link:first').hide();  

If you need to work only on the first element

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文