為啥這樣不能簡化呢?
<script>
? ? ? ? ? ? ?$("li:gt(4)").hide();
? ? ? ? ? ? ?
? ? ? ? ? ? ?$("a:contains(更多)").click(function(){
? ? ? ? ? ? ? ? $("li:gt(4)").show();
? ? ? ? ? ? ? ? $(this).text("簡化");
? ? ? ? ? ? ?});
? ? ? ? ? ? ?
? ? ? ? ? ? ?$("a:contains(簡化)").click(function(){
? ? ? ? ? ? ? ? $("li:gt(4)").hide();
? ? ? ? ? ? ? ? $(this).text("更多");
? ? ? ? ? ? ?});
? ? ? ? </script>
可以正常的展開更多,但是不能簡化。。。
2016-05-13
頁面加載一次全部都加載,$("a:contains(更多)")指向元素a沒問題,可以觸發事件,但是后面并沒有找到對象;
所以不會觸發事件,如果一定這么些,可以把這段代碼放在一個setInterval中
2016-05-13
我剛剛試了很久,發現contains會有問題,你自己寫個alert試下,每個點擊里面就加個alert。你會發現,每次點擊那個a標簽內容,彈出的都是更多的那個,也就是說后面雖然把那個a標簽內容改為了“簡化”,但是它其實contains到的還是更多。具體原因我也不曉得。我是試出來的。
2016-05-13
<script type="text/javascript">
? ? $(function()
{
$("li").hide();
$("li:lt(5)").show();
$("a").click(function()
{
var a=$("a");
if(a.html()=="更多")
{
$("li").show();
a.html("簡化");
}
else
{
$("li").hide();
$("li:lt(5)").show();
a.html("更多");
}}
);
});
</script>