简体   繁体   English

如何隐藏/删除两个元素之间的所有文本?

[英]how to hide/remove all text in-between two elements?

lets say i have this lump of HTML: 让我说我有这个HTML块:

<div class="container">
<span class="title">Heading 1</span>
This is a description..<br />
This is the second line..
<div class="image"><img src="image.jpg" /></div>
</div>

What i want to do using jQuery/JavaScript is hide/remove all text and elements between <span class="title"> and <div class="image"> . 我想用jQuery / JavaScript做的是隐藏/删除<span class="title"><div class="image">之间的所有文本和元素。

I've looked all over and found pretty much nothing. 我看了一遍,几乎找不到任何东西。 Any ideas on how i could do this? 关于我如何做到这一点的任何想法?

How about something like this? 这样的事怎么样? http://jsfiddle.net/ZW8q2/1 http://jsfiddle.net/ZW8q2/1

var foo = $('.container').children(':not(br)');
$('.container').html('').html(foo);

You could try: 你可以尝试:

var str = $('.container .title').text() + $('.container .image').html();
$('.container').html(str);

JS Fiddle . JS小提琴

Try this: 尝试这个:

Place a tag around what you want to hide, give div an ID name. 在要隐藏的内容周围添加标记,为div指定ID名称。 So in the end your will look like: 所以最后你会看起来像:

<div id = "hideMe" style ="display:block">...Your Content...</div>

Use this javascript: 使用此javascript:

    function hide()
    {
    document.getElementById('hideMe').style.display = "none";
    return;
    }

Have the Javascript function called whenever you want to hide the stuff between the div from a button (or other control) like this: 每当你想在按钮(或其他控件)之间隐藏div之间的东西时调用Javascript函数,如下所示:

<input type= "submit" onclick ="hide()">

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

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