“打开”/占卜 HTML 段落
"Opening"/Divinding an HTML paragraph
这个问题可能是一个有趣的问题......在显示文本的网页上,我需要一个特定的行为:当用户点击一些特殊单词时,该单词的解释会在点击的单词下方打开。所有的问题是:它应该真正在单词下方打开,就像将段落"打开"成两段,而不是让后面的文本跳到下一行。
我找到了一个使用 CSS float 属性运行良好的解决方案。你可以在那里看到它(会比下面的代码说得更多(:http://jsfiddle.net/3gwzx/3/
此解决方案的主要问题是它使用嵌套跨度。而且,由于 span 不是块标签,因此填充不适用于它们。因此,在灰色框中,文本永远不会有任何水平填充(垂直填充是可以的( - 否则它会改变框本身的大小 - 还是我错过了什么?- 这很糟糕。有人有解决方案吗?我应该以另一种方式重新思考这个问题吗?
非常感谢!
以下是 HTML 的外观:
<body onload="putListeners()">
<p>This is the normal text here, you cannot click it. But some words needs long
explanations, like this one : <span class="note">click on me
<span class="noteTxt">The explanation begins here <a class="foo" href="bar.html"> and
could have link</a> that one should be able to click.
And the note can be loooong, long, long, very, very long.
</span>
</span>
And here, the text carry on. It could carry on for a long, long time.
And with all the other solutions I tried, this part of the text "jumps" after the note,
on a new line, when it appears. What I like here is that, when you open the note,
it really "open the paragraphs in two". But i cannot give a padding
to those nested span… I should have a div… but you cannot put a div
inside a span !</p>
</body>
这是 JS
function putListeners() {
//just listens to the text…
var elements = document.getElementsByClassName("note");
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", showNote, false);
}
};
function showNote()
{
//content to show
var currentTxt;
//finds the nested noteTxt
for (var i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i].className == "noteTxt") {
currentTxt = this.childNodes[i];
break;
}
}
//displays it or hides it
if(currentTxt.style.display=="none" || currentTxt.style.display=="")
{
currentTxt.style.display="block";
}
else
{
currentTxt.style.display="none";
}
return true;
};
并且,有关信息,CSS 的相关部分(您可能已经弄清楚了它的外观 - Jfiddle 中的完整代码(:
span.note {
position: static;
}
span.note span.noteTxt {
display: none;
float: left;
color: black;
width: 100%;
background-color: #eaeaea;
}
如果要
更改标记的布局行为,在<span>
的情况下,可以设置 css 属性display: block
将其更改为div 样式布局。
span.asblock {
display: block;
}
这将为您提供一个行为类似于div 的跨度。
相关文章:
- 在HTML字符串的某个段落后插入HTML
- 我怎么能在同一个HTML页面上多次在不同的段落中添加相同的字符串呢
- 如何隐藏和显示 html 输入文本和段落
- 如何获取段落的文本,然后将其转换为 HTML 中的整数
- 正则表达式用于匹配段落中的单词,但排除内部 HTML 标记
- “打开”/占卜 HTML 段落
- 内部文本不适用于段落和标题 HTML 元素
- 无法在网页上显示的 HTML 段落上叠加图像图标
- 是否可以在没有html锚点的情况下找到网页的某些段落
- 如何使用 jQuery 将段落的原始文本格式放入 HTML 段落元素中
- 仅在单击时显示段落的 HTML
- 使用 JavaScript 组合 HTML 段落
- JavaScript/HTML|使HTML段落等于用户输入
- 如何让我的javascript var在html段落中返回
- 计算HTML段落的字数
- 如何获得html段落值的php变量
- 列出一个HTML段落< >在一个时间效应
- 如何在.html段落中显示.js文件中的数组元素
- 如何将一个HTML段落元素的文本值传递给另一个HTML段落元素
- 有效的方式来选择和动态样式长的HTML段落