简体   繁体   English

jQuery hide(),show()或html()

[英]jQuery hide(), show() or html()

I have a 我有一个

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

and three js variables that store different html: content1 , content2 and content3 . 和三个存储不同html的js变量: content1content2content3

By user interactions, the content of mentioned above div changes to one of that that stored in js variables. 通过用户交互,上述div的内容更改为js变量中存储的内容之一。

What is preferable either to directly set div content to what I need by user interaction: 最好是通过用户交互将div内容直接设置为我需要的内容:

$("#content").html(content2);

or to change div structure to: 或将div结构更改为:

<div id="content">
    <div id="c1">
        // value of content1 variable here
    </div>
    <div id="c2">
        // value of content2 variable here
    </div>
    <div id="c3">
        // value of content3 variable here
    </div>
</div>

And doing hide() and show() to that inner blocks, ie when I want content2 to be shown: 然后对该内部块执行hide()show() ,即当我希望显示content2时:

$("#c1").hide();
$("#c2").show();
$("#c3").hide();

?

I'd say hiding & showing divs. 我会说隐藏并显示div。

It's less intensive, and if the content inside the javascript variables happens to contain elements that you'll bind to, you won't have to rebind everytime you refresh the content, and if you wanted to have some sort of animation between the different content, multiple divs also allows that. 它的强度较低,并且如果javascript变量中的内容恰好包含要绑定的元素,则每次刷新内容时都不必重新绑定,并且如果您希望不同内容之间具有某种动画效果,多个div也允许这样做。

As a side note, using jQuery it's less code to do something like 附带一提,使用jQuery可以减少执行类似操作的代码

$("#c2").show().siblings().hide();

The two aren't really all-that comparable since they do different things. 两者并不是真正的全部-可比较,因为它们做的是不同的事情。 They may well give a similar perception but what's happening isn't the same in terms of markup. 他们可能会给出相似的看法,但是就标记而言,发生的事情并不相同。 In fact, it's not uncommon to see .html('Something').show() chained together. 实际上,看到.html('Something').show()链接在一起的情况并不少见。

  • Passing a string to .html() replaces the content of the selected element, it does nothing to affect the element itself. 将字符串传递给.html()替换所选元素的内容 ,它不会影响元素本身。
  • Calling .show() or .hide() only affects the element itself - all the descendants remain exactly the same, they just can't be seen because their parent is not being displayed. 调用.show().hide() 影响元素本身-所有的后代保持完全相同,他们只是因为没有显示他们的父母都看不出来。

By using .html() you are replacing everything inside your element. 通过使用.html()您将替换元素中的所有内容。 All references to these descending elements will become undefined and direct (non-delegated) event listeners will also be lost. 对这些降序元素的所有引用将变为未定义,并且直接(未委托)事件侦听器也将丢失。

.hide() and .show() do exactly what they say. .show() .hide().show()完全按照他们说的做。 The data inside your element is still preserved, the event handlers still in place, it's all just 'hidden' by way of display: none . 您元素中的数据仍然保留,事件处理程序仍然保留,通过display: none全部被“隐藏” display: none

If the content dynamically changes, without page-load, use .html() , if not, .show() and .hide() are more appropriate. 如果内容是动态变化的,没有页面加载,使用.html()如果没有, .show().hide()是比较合适的。

For the ease of use and shorter more cleaner looking code, setting the content through HTML is the right option! 为了易于使用和使代码看起来更简洁,使用HTML设置内容是正确的选择!

Think of it as what you're trying to do, 1 DIV => Can contain 3 different contents, you can manipulate it through JS. 将其视为您要尝试的操作,1 DIV =>可以包含3种不同的内容,您可以通过JS对其进行操作。

So, in your first solution, you actually have one div and manipulating it through JS: 因此,在第一个解决方案中,实际上有一个div并通过JS进行操作:

$("#content").html(content1);
$("#content").html(content2);
$("#content").html(content3);

Whereas, in the second solution, you are actually using 4 divs for the same functionality! 而在第二种解决方案中,您实际上是使用4个div来实现相同的功能! So definitely, if you can do something with 1 div. 所以可以肯定,如果您可以对1 div进行处理。 That's the preferred way. 那是首选的方式。

They both are taking equal lines for JS, but with the second approach, your HTML will contain a lot more code considering your contents are large. 两者对于JS来说都是平等的,但是使用第二种方法,考虑到内容很大,HTML将包含更多代码。

I think that the best solution is to store the different contents into three variables and then assign to the div the choosen one with 我认为最好的解决方案是将不同的内容存储到三个变量中,然后将选定的内容分配给div

$("#content").html(content2);

In this way you have three less nodes on your DOM tree 这样,您的DOM树上少了三个节点

There isn't that much difference between the two options. 两种选择之间没有太大区别。 One factor that might affect this is the actual size of the content you are changing. 可能会影响此的一个因素是您要更改的内容的实际大小。 If the content is relatively small then it really doesn't matter which way you choose. 如果内容相对较小,那么选择哪种方式都没关系。

Another thing to consider is how available the three versions of the content variable is. 要考虑的另一件事是content变量的三个版本的可用性。 If you have to fetch this HTML content each time you load it then it might make sense to pre-populate the content before you display it to your users so as to save the time it takes to load it. 如果每次加载时都必须获取此HTML内容,则在将其显示给用户之前预先填充该内容可能很有意义,这样可以节省加载时间。 Then just show/hide the appropriate content. 然后只显示/隐藏适当的内容。

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

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