简体   繁体   English

如何将文本更改为不可见,但仍显示下划线?

[英]How to change text to invisible, but still show underline?

I made a studying tool using Javascript and PHP.我使用 Javascript 和 PHP 制作了一个学习工具。 There's a toggle that shows/hides keywords in a paragraph, so the user can mentally "fill in the blanks".有一个切换显示/隐藏段落中的关键字,因此用户可以在精神上“填空”。

How I've done this so far is that the all the keywords are underlined, and I use a DOM selector to select all innerHTML in u tags.到目前为止,我是如何做到这一点的,所有关键字都带有下划线,并且我使用 DOM 选择器来选择 u 标签中的所有 innerHTML。

<p>
  Example sentence, <u>this</u> is a <u>keyword</u>.
</p>
<button onClick='hideKeywords()'>Hide Keywords</button>

<script>
  function hideKeywords() {
    var x = Array.from(document.getElementsByTagName('u'));
    for (var i = 0; i < x.length; i++) {
      x[i].style.visibility = "hidden";
    }
  }
</script>

This works as intended - the "keywords" are blanked out and the flow of the document is unaffected, since the keywords still take up the same space that they would normally take.这按预期工作 - “关键字”被清除并且文档流不受影响,因为关键字仍然占用它们通常占用的相同空间。

One downside is that in paragraphs with particularly long "keywords", the paragraph's line structure is disrupted and it looks like text is just floating randomly in space.一个缺点是,在“关键字”特别长的段落中,段落的行结构被打乱,看起来文本只是在空间中随机浮动。 This would be fixable if I could somehow change visibility such that the words in the keywords are hidden and the text-decoration (underline) still shows.如果我能以某种方式改变可见性,使得关键字中的单词被隐藏并且文本装饰(下划线)仍然显示,这将是可以解决的。 This retains the line structure.这保留了线路结构。

I thought about using Javascript to replace every character in the keyword with underscores, but two more problems pop up.我想过使用 Javascript 用下划线替换关键字中的每个字符,但又出现了两个问题。 One thing is that even if the number of characters stay the same, the width might change.. For example, "apple" is not the same physical length as "______".一件事是,即使字符数保持不变,宽度也可能会发生变化。例如,“apple”与“______”的物理长度不同。 This is not ideal as the document flow would change.这并不理想,因为文档流会发生变化。 A second problem is that I can't think of any way to get the keywords back after converting them into underscores.第二个问题是,在将关键字转换为下划线后,我想不出任何方法将它们取回。

One workaround is instead of changing the visibility to "hidden", I could change the background-color to the same color as the text.一种解决方法是,我可以将背景颜色更改为与文本相同的颜色,而不是将可见性更改为“隐藏”。 This blocks out the text, but the line structure and document flow are both preserved.这挡住了文本,但保留了行结构和文档流。 However, I do hope to find a way to implement my original idea, so any suggestion is appreciated!但是,我确实希望找到一种方法来实现我最初的想法,所以任何建议都值得赞赏!

Update: I would prefer not to add any additional divs to the keywords.更新:我不想在关键字中添加任何额外的 div。 The user can add new entries using a rich text editor, so declaring a keyword is as easy as underlining it in the text editor.用户可以使用富文本编辑器添加新条目,因此声明关键字就像在文本编辑器中加下划线一样简单。

You can do it with css adding a pseudo-element and instead of using visibility hidden, using color: transparent, like this:您可以使用 css 添加一个伪元素,而不是使用隐藏的可见性,使用颜色:透明,如下所示:

u{
  position:relative;
}
u::after{
  content: ' ';
  display:block;
  position:absolute;
  bottom:0;
  left:0;
  width: 100%;
  height:1px;
  background-color:#000;
}

And in the script并且在脚本中

<script>
  function hideKeywords() {
    var x = Array.from(document.getElementsByTagName('u'));
    for (var i = 0; i < x.length; i++) {
      x[i].style.color = "transparent";
    }
  }
</script>

https://codepen.io/anon/pen/ROoMaM https://codepen.io/anon/pen/ROoMaM

I would probably implement this using your second method.我可能会使用您的第二种方法来实现这一点。 Set the text colour to the same as the background then add a bottom border to the element.将文本颜色设置为与背景相同,然后为元素添加底部边框。 Preserves the spacing and allows you to quickly check if you are right by just highlighting the line.保留间距,让您只需突出显示该行即可快速检查您是否正确。 Also, if you give the keyword wrapper element a class you can easily just toggle the color of the hidden text, and retain the underlines so you can see what has changed.此外,如果您给关键字包装元素一个类,您可以轻松地切换隐藏文本的颜色,并保留下划线,以便您可以看到发生了哪些变化。

 div { color: black; } span { color: white; text-decoration: none; border-bottom: 2px solid green; }
 <div> This <span>is hidden</span> with an underline </div>

You'd have to find some way to get the div positioned under the hidden text, but you can use the following code to create a div with the width of the text.您必须找到某种方法将 div 定位在隐藏文本下,但您可以使用以下代码创建一个具有文本宽度的 div。 I might come back later after answering and find a way to position the div.我可能会在回答后回来并找到一种定位 div 的方法。

In your HTML, let's say you have a phrase, foo bar , that you want hidden.在您的 HTML 中,假设您有一个想要隐藏的短语foo bar You will assign an id to it with the code: <p id="foo">foo bar</p>您将使用以下代码为其分配一个 id: <p id="foo">foo bar</p>

Here is the CSS:这是CSS:

#foo {
    position: absolute;
    visibility: hidden;
    height: auto;
    width: auto;
    white-space: nowrap;
}

Then in your Javascript, you can use the following code:然后在您的 Javascript 中,您可以使用以下代码:

var fontSize = 20;                          //this defines a font size   
var foo = document.getElementById("foo");
foo.style.fontSize = fontSize;              //this sets the style as the font size
var width = (foo.clientWidth) + "px"

var div = document.createElement("div");
div.style.width = width;
div.style.height = 2px;
div.style.background = "red";      //makes the underline visible

document.getElementById("main").appendChild(div);

From here, you could probably just reposition the div how you want so it appears under the text.从这里开始,您可能只需根据需要重新定位 div,使其显示在文本下方。 This way, the div is the exact length of the text.这样,div 就是文本的确切长度。

An alternative solution would be to use a monospace font and then manually calculuate the width.另一种解决方案是使用等宽字体,然后手动计算宽度。

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

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