简体   繁体   English

使用jQuery隐藏div中的内联文本

[英]Hide inline text in div using jQuery

How can I hide the inline text within the div of class .test without hiding the h1 element? 如何隐藏.test类的div中的inline文本而不隐藏h1元素? This is a simplified example of a problem I'm having in a larger web application. 这是我在较大的Web应用程序中遇到的问题的简化示例。

Some other guidelines are that I can't wrap the text in a span tag and I want to keep all of the content in the div so I can't simply erase and add back everything I want. 其他一些准则是,我不能将文本包装在span标签中,并且我希望将所有内容保留在div中,因此我不能简单地擦除并重新添加想要的所有内容。

 $(document).ready(function() {}); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> 

You need to wrap text in another element, for example span. 您需要将文本换行到另一个元素中,例如span。

<div class="test">
  <h1>Hello World</h1>
  <span class="my-text">"This is text"</span>
</div>

And your js: 和你的js:

$(document).ready(function() {
  $('.my-text').hide();
});

If fore some reason you are not able to wrap the text (as you mentioned in the comments), you can use another simple solution: 如果出于某些原因您不能包装文本 (如注释中所述),则可以使用另一个简单的解决方案:

<div class="test">
  <h1>Hello World</h1>
  This is text
</div>

And js: 和js:

$(document).ready(function() {
  var h1elem = $('h1'); //cache element which should not be removed
  $('.test').empty();  //clear container .test
  $('.test').append(h1elem); // append cached element back
});

Here is the plunker for you. 这是给你的矮人 (2 seconds timeout added for better visualising). (添加了2秒的超时时间以实现更好的可视化效果)。

If you can't add an element around the "This is text" string, then you can store the H1 element in a variable and then replace the .text HTML with the stored H1 element. 如果不能在“ This is text”字符串周围添加元素,则可以将H1元素存储在变量中,然后将.text HTML替换为存储的H1元素。

 $(document).ready(function() { var h1 = $('.test').find('h1'); $('.test').html(h1); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> 

EDIT 编辑

Seeing as you want to keep other unknown content in the DIV then you oculd do this. 如果您想将其他未知内容保留在DIV中,则可以执行此操作。

 $(document).ready(function() { var h1 = $('.test').find('*'); $('.test').html(h1); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" <h2>blah blah blah</h2> <p>Random content</p> <ul> <li>list</li> <li>list</li> </ul> </div> 

hidden with JQuery 用jQuery隐藏

 var Elem= $(".test h1"); $(".test h1").remove(); var Text=$(".test").text(); $(".test").empty().html(Elem).append($("<span/>",{text:Text,style:"display:none"})); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> 

hidden with CSS 用CSS隐藏

 .test{font-size:0; } .test h1{font-size:35px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> 

Actually you can not say I want to hind entire div but not a single or 2 tags inside it. 实际上,您不能说我想阻止整个div,但不能在其中包含单个或2个标签。 So you have to hide or show all of the inside that area 因此,您必须隐藏或显示该区域内的所有区域

so if you want to show h1 but hide span then you can do this way 因此,如果您想显示h1但隐藏跨度,则可以这样做

$(document).ready(function() {
    $('.test').find('h1').style('display', 'block');
    $('.test').find('span').style('display', 'none');
    // add all the tags you want to operate
});

Using Childern should work 使用Childern应该有效

var child = $('.test').children();
 $('.test').html(child);

//OR
$('.test').html($('.test').children());

JS Fiddle JS小提琴

You can use the below function to wrap the text in a span and then perform the show hide functionality. 您可以使用以下功能将文本包裹在一个跨度中,然后执行显示隐藏功能。

 $(document).ready(function() { //the below line fetches only the plain text inside the element var text = $('.test').clone().children().remove().end().text(); //the below line fetches the html inside the element var html = $('.test').clone().children(); //append the html and the text with a span wrapped around it $('.test').html(html).append('<span>' + text + '</span>'); //you can then add any CSS you want to show hide the contents! $('button').on("click", function() { $('.test > span').css("display", "inline"); }); }); 
 .test>span { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> <button>show</button> 

You can't hide the text without wrapping in another element. 如果不包装其他元素,则无法隐藏文本。

wrap it in the span and after that, you can play with that span 将其包裹在范围内,然后,您可以使用该范围

<div class='test'>
  <h1>Hello World</h1>
  <span id='test-span-id'>This is Text</span>
  <span class='test-span-class'>This is Text</span>
</div>

Here is your JS file 这是您的JS文件

$(document).ready(function(){
    $('.test-span-class').hide();
    $('#test-span-id').hide();
});

I would suggest to use ID Selector instead of class Selector for better performance. 我建议使用ID选择器而不是类选择器以获得更好的性能。

you can go through to link, why I have suggested to use ID Selector. 您可以进行链接,为什么我建议使用ID选择器。

In jQuery, is selecting by class or id faster than selecting by some other attribute? 在jQuery中,按类或id进行选择是否比按其他属性进行选择更快?

You don't need to wrap it in HTML. 您无需将其包装为HTML。 This should work. 这应该工作。

 $(document).ready(function() { $('.test') .contents() .filter(function() { return this.nodeType === 3; }).wrap( '<div class="hidden"></div>' ); }); 
 .hidden{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test"> <h1>Hello World</h1> "This is text" </div> 

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

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