简体   繁体   English

如何隐藏未包装的文本 <p> 还是<span>?</span>

[英]How do I hide text that is not wrapped in <p> or <span>?

I have html where I only want to hide the text portion, but what complicates it is that it is not wrapped in anything. 我有html我只想隐藏文本部分,但让它变得复杂的是它没有包装在任何东西中。 Below is an example: 以下是一个例子:

<div class="content">

    Give comfortable and durable place to work with a desk.
    Lock the center drawer and all of the drawers lock,
    keeping your files and supplies secure.

    <table cellpadding="4" cellspacing="0">
        <tbody>
            <tr>
                <th align="right">Available Options:</th>
                <td>Desktop Color: Fusion Maple, Gray Nebula...</td>
            </tr>
            <tr>
                <th align="right">Book Box Construction:</th>
                <td>Not applicable</td>
            </tr>
        </tbody>
    </table>
</div>

How can I hide the "Give comfortable and durable..." portion above without changing the html at all? 如何在不更改html的情况下隐藏上面的“给予舒适和持久...”部分? I was hoping to do it via CSS and maybe Javascript if necessary. 我希望通过CSS做到这一点,如果有必要,可能是Javascript。 CSS only would be perfect though. 但CSS只是完美的。 Can anyone help? 有人可以帮忙吗?

Look ma', no hands scripts! 看马',没有手脚本

CSS only solution CSS唯一解决方案

.content
{
    text-indent: -1000em;
}

.content *
{
    text-indent: 0;
}

/* This one's pure cherry on top ;) */
/* Remove excess vertical space */
.content *:first-child
{
    margin-top: -1em;
}

JSFiddle demo . JSFiddle演示

CSS defines the appearance of the contents of a tag. CSS定义标记内容的外观。 No tag, no styling. 没有标签,没有造型。

You need to modify the HTML. 您需要修改HTML。 Sorry. 抱歉。

This removes the text node, which is a container for the text: 这将删除文本节点,该节点是文本的容器:

var div = document.getElementsByTagName('div')[0];
div.removeChild(div.firstChild);

Using javascript (and jQuery in my case): 使用javascript(和我的jQuery):

var table = $('.content table'); //copy the table
$('.content').html(table); //replace the html of div.content with table

If you have multiple classes of content then you'll need to rewrite this a bit to work over a collection 如果您有多个类别的content那么您需要重写一下以处理集合

There is no easy way for that. 没有简单的方法。 You'll have to write an html stripper on JavaScript or simply modify your html by hands. 你必须在JavaScript上编写一个html脱衣舞,或者只是手工修改你的html。

CSS: CSS:

#content {
  visibility: hidden;
}

JavaScript: JavaScript的:

document.getElementsByTagName("div")[0].style.visibility = "hidden";
or:
document.getElementsByClassName("content")[0].style.visibility = "hidden";

暂无
暂无

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

相关问题 如何检测所选文本是否包含在 span 标签中 - How to detect if selected text is wrapped in span tags 当必须用撇号包装时,如何隐藏 API 密钥? - How do i hide API key when it must be wrapped in apostrophe? 我该如何隐藏<li>当<span>里面包含某些文本时?</span></li> - How do I hide <li> when the <span> inside contains certain text? 如何使提交按钮在 html 中“打印”内容,并在与 a 相同的行中输入宽度为 100% 的文本<span>,</span><p> <span>, 或者</span><div> <span>?</span> - How do I make submit buttons 'print' things in html and make a text input with 100% width in the same line as a <span>, <p>, or <div>? 如何在文本范围内居中? - How do I center text within span? 如何将 Twitter Bootstrap 工具提示应用于包裹在<span>.</span> - How to apply a Twitter Bootstrap tooltip to text wrapped in a <span>? 使用 HTML 按钮和 Javascript 切换显示/隐藏包含在 Span class 标签中的 html 文本 - Toggle show/hide html text wrapped in Span class tags using HTML button and Javascript 如何在可编辑div中输入和删除预跨行包装的文本中的文本,同时保留span标签 - How to type and delete text in a pre-span-wrapped text in conteteditable div while keeping the span tag 如何隐藏除跨度jQuery之外的ap标签中的所有内容? - How to hide everything inside a p tag except span jQuery? 如果单词尚未包装,如何将它们包装到 span 元素中? - How can I wrap words into span elements if they are not already wrapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM