大家好,我有这个脚本
<a class="clickMe" toggle="first">Text 1</a>
</br>
<div id="first" class="text"> - This text will be toggled</div>
<a class="clickMe" toggle="second">Text 2</a>
</br>
<div id="second" class="text"> - This text will be toggled 2</div>
和jquery
$('a.clickMe').click(function () {
id = $(this).attr('toggle');
$('.text').not('#' + id).hide();
$('#' + id).show();
});
当我们点击Text 1
或Text 2
时,通过切换This text will be toggled
或This text will be toggled 2
,上面的代码可以很好地工作,请检查演示
但现在我想做的是为切换的文本设置固定的位置/对齐方式,现在它被切换到文本1或文本2下方,但我需要把它放在右侧,这样当文本1或2中的任何一个被点击时,toggled语句都应该显示在同一位置(右侧而非下方)。。。请帮我解决这个问题。。。。
您可以使用像span
和toggle
方法这样的非块元素。
<a class="clickMe">Text 1</a>
<span class="text"> - This text will be toggled</span>
<br/>
<a class="clickMe">Text 2</a>
<span class="text"> - This text will be toggled 2</span>
$('a.clickMe').click(function () {
$(this).next().toggle()
});
http://jsfiddle.net/LTYa2/66/
为什么不使用jQuery的.text()
功能进行尝试:
http://jsfiddle.net/FlameTrap/pe8xV/2/
你可以试试这个:
div {
display: inline-block;
}
此处是已更正的工作实况演示。