在所有浏览器中是否有HTML字符(包括无空格)? [英] Is there a HTML character that is blank (including no whitespace) on all browsers?

查看:160
本文介绍了在所有浏览器中是否有HTML字符(包括无空格)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有HTML字符,在所有(主要)浏览器(加上IE8可悲)上都不显示任何内容,并且不会增加额外的空间?

可以替代& nbsp; ,但它不会将空白添加到页面,并且不会显示为丑陋的无法识别的字符标记或<$ c



为什么:在我的情况下,我正在努力工作围绕一个旧的专有CMS的问题,该CMS将删除空的但必需的HTML元素,因为系统的其他部分将动态填充它们。



想象一些类似于(简化的简单示例)< span class =placeholderdata-type =username>< / span> ; 如果用户登录,用用户名填充 - 但这个老派CMS认为它是空的并将其删除。

解决方案

似乎有两个选项主要适合该法案。他们似乎在< span> 中可靠地不显示任何内容,但他们(特别是第二个选项)可能会对复制/粘贴和分词产生较小影响个案。

零宽空间



&#8203; aka &#x200B; 其行为与(



在一个范围内,它似乎永远不会添加连字符(但如果可能的话,最好使用零宽度空格)。




编辑:我发现了一个较旧的答案,将这些答案作为解决不同问题的讨论,这表明这些答案是健壮的除了可能的复制/粘贴怪癖。



我在研究中发现的唯一其他问题是显然一些搜索引擎可能会将含有这些词的单词视为被拆分(例如 awe& shy;一些可能与 awe 和一些而不是 awesome )。


Is there a HTML character that, on all (major) browsers (plus IE8 sadly) displays nothing and doesn't add any extra space?

So, an alternative to &nbsp; but which doesn't add whitespace to the page, and which won't ever show up as an ugly "unrecognised character" marker or ?.


Why: in my case, I'm trying to work around a problem on an old, proprietary CMS that is removing empty but necessary HTML elements that are required because other parts of the system will fill them dynamically.

Imagine something like (simplified trivial example) <span class="placeholder" data-type="username"></span> which is populated with a user's username if a user is logged in - but this old-school CMS sees it as being empty and removes it.

解决方案

There seem to be two options that mostly fit the bill. They seem to reliably not show anything when in a <span>, but they (particularly the second option) might have a minor effect on copy/paste and word breaking in some cases.

Zero-width space

&#8203; aka &#x200B; which behaves the same as the (now in HTML5) <wbr> - used to make words break at certain points without changing the display of the words.

<h1>This text is full<span>&#8203;</span> of spans with char<span>&#8203;</span>acte<span></span>rs that affe<span>&#8203;</span>ct word brea<span></span>king but don't show up</h1>
<h1>Especially in das super<span>&#8203;</span>douper&#8203;crazy<span>&#8203;</span>long<span></span>worden.</h1>

Seems to work fine on modern browsers and IE7+ (not tested on IE6).


Soft hyphen

&shy; - like a zero-width space but (in theory) adds a hyphen when it breaks a word across a line.

<h1>This text is full<span>&shy;</span> of spans with char<span>&shy;</span>acte<span></span>rs that affe<span>&shy;</span>ct word brea<span></span>king but don't show up</h1>
<h1>Especially in das super<span>&shy;</span>douper&shy;crazy<span>&shy;</span>long<span></span>worden.</h1>
<h1>Example where das super&shy;douper&shy;crazy&shy;longword contains no spans.</h1>

Fine on modern browsers and IE7+ (not tested on IE6), though as some comments note there are issues with these turning into regular hyphens when copied and pasted, for example, here's how it pastes from Chrome to Notepad, on Windows 8.1:

Within a span, it seems to never add a hyphen (but still better to use zero-width spaces if possible).


Edit: I found an older SO answer discussing these as a solution to a different problem which suggests these are robust except for possible copy/paste quirks.

The only other issue with these I could find in research is that apparently some search engines may treat words containing these as being split (e.g. awe&shy;some might match searches for awe and some instead of awesome).

这篇关于在所有浏览器中是否有HTML字符(包括无空格)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆