这个问题可能是一个有待解决的问题...... 在显示文本的网页上,我需要一个特定的行为:当用户点击某些特殊单词时,会在单击的单词下方打开对该单词的解释。所有的问题是:它应该在单词下方打开,比如“打开”两段中的段落,而不使后面的文本跳转到下一行。
我发现了一个使用CSS浮动属性的解决方案。 你可以在那里看到它(比下面的代码更多):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;
}
答案 0 :(得分:3)
如果要更改标记的布局行为,在您的情况<span>
中,您可以设置css属性display: block
以将其更改为div样式布局。
span.asblock {
display: block;
}
这会给你一个像div一样的跨度。