如何识别被其他元素或标签包围的组元素?

发布于 2024-08-20 05:16:11 字数 787 浏览 5 评论 0原文

我有问题请参考以下代码来理解问题。 (我从下面的 html 代码中删除了 '<' 和 '>' 字符,否则 html 标签将不可见,所以我只写了标签名称)

<div>
    <div>
       <img />
       <img />
       <img />
       <img />
    </div>
</div>

jQuery 的帮助下解决以下任务

  1. 我想在文档准备好的 内部 div 之间的四个 IMG 中,只有第一个 IMG 应该可见,其余三个 IMG 应隐藏。
  2. 现在,在特定事件(例如焦点、单击等)上,内部 div 之间的四个 img 中的可见 img 会隐藏,而另一个应该可见。

其他一些问题:

  1. jQuery 是否只能识别其他标记包围的元素?

  2. 我还想知道 jQuery 中的控制流如何?特别是在链式函数中。 对于 EX。

    1. $(选择器).fun1(val,{fun2(){ }} 在上面的示例中,哪个函数首先执行以及按什么顺序执行。

    2. $(选择器).fun1().fun2().fun3() 在上面的示例中,哪个函数首先执行以及按什么顺序执行。

    3. 函数链中的函数按照什么顺序执行?

等待你们的回复!

I have question please refer the following code to understand the question. (I removed '<' and '>' character from the following html code because otherwise the html tags will not be visible so i have written only tag names)

<div>
    <div>
       <img />
       <img />
       <img />
       <img />
    </div>
</div>

I want to solve following task with the help of jQuery

  1. On document ready out of four img between the inner div only first img should be visible and rest of the three img should get hidden.
  2. Now on particular event like focus, click etc. the out of four img between the inner div the visible img get hide and the other one should get visible.

Some other questions:

  1. Is jQuery is able to identify only those elements enclosed by other tag?

  2. I also want to know how the control flows in jQuery? specially in chained function.
    for EX.

    1. $(selector).fun1(val,{fun2(){ }}
      in above example which function is get executed first and in what sequence.

    2. $(selecter).fun1().fun2().fun3()
      in above example which function is get executed first and in what sequence.

    3. In what sequence the functions in function chaining is get executed?

Waiting for your reply guys!

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

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

发布评论

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

评论(1

谜兔 2024-08-27 05:16:11

尝试像我在此处所做的那样。


根据您的要求,第一张图片(twitter)不会改变。 类的图像

唯一受影响的图像是 div 中具有 sample HTML

<img src="https://s3.amazonaws.com/twitter_production/a/1265328866/images/twitter_logo_header.png"/>

<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>

<div class="sample">
  <img src="http://sstatic.net/so/img/logo.png">
  <img src="http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif">
  <img src="http://cacm.acm.org/images/logo.ACM.gif">
  <img src="http://static03.linkedin.com/img/logos/logo_linkedin_88x22.png">
</div>

JavaScript

$(function () {
    var textboxes = $("input:text"), //gets all the textboxes         
        images = $(".sample img");   //gets all the images

    images.not(":first").hide(); //hide all of them except the first one
    textboxes.each(function (i) {
        var j = i;
        $(this).focus(function () {
            images.hide().eq(j).show();
        });
    });
});

Try something like I did here.


The first image (twitter) does not change, as per your requirements. The only images that are affected are the ones in the div that has the class sample

HTML

<img src="https://s3.amazonaws.com/twitter_production/a/1265328866/images/twitter_logo_header.png"/>

<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>

<div class="sample">
  <img src="http://sstatic.net/so/img/logo.png">
  <img src="http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif">
  <img src="http://cacm.acm.org/images/logo.ACM.gif">
  <img src="http://static03.linkedin.com/img/logos/logo_linkedin_88x22.png">
</div>

JavaScript

$(function () {
    var textboxes = $("input:text"), //gets all the textboxes         
        images = $(".sample img");   //gets all the images

    images.not(":first").hide(); //hide all of them except the first one
    textboxes.each(function (i) {
        var j = i;
        $(this).focus(function () {
            images.hide().eq(j).show();
        });
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文