2014-03-01 21 views
1
<script type="text/javascript"> 
var button = document.getElementById("button"); 
var idTab5 = document.getElementById("idTab5"); 
var button2 = document.getElementById("button2"); 

function showwMore() { 
    button.style.display="none"; 
    idTab5.style="overflow:hidden;display:block;height:100%;font-size:12px;line-height:14px;"; 
     button2.style.display="block"; 

} 

function showwLess() { 
    button2.style.display="none"; 
    idTab5.style="overflow:hidden;height:250px;font-size:12px;line-height:14px;"; 
    button.style.display="block"; 
} 
</script> 

,這是其他代碼:的Javascript:上按鈕改變股利大小單擊

{if $product->description|count_characters:true > 350 } 
     {* full description *} 
     <div id="idTab5" style="overflow:hidden;height:250px;font-size:12px;line-height:14px;">{$product->description}</div> 
     <input id="button" type="button" style="margin-top:5px;font-size:12px;color:white; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar +" onclick="showwMore()"> 
     <input id="button2" type="button" style="margin-top:5px;display:none;font-size:12px;color:white; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar -" onclick="showwLess()"> 
    {else} 
     <div id="idTab5" style="overflow:hidden;height:250px;font-size:12px;line-height:14px;">{$product->description}</div> 
    {/if} 

現在它似乎沒什麼問題,但的onclick,股利心不是越來越大,任何想法,爲什麼會出現這種情況?香港專業教育學院多次檢查代碼無法找出爲什麼它不工作,我錯過了什麼?

回答

0

對於使用多個樣式由JavaScript可以使用setAttribute()

應該

idTab5.setAttribute("style", "overflow:hidden;display:block;height:100%;font-size:12px;line-height:14px;"); 

或者

可以使用cssText屬性元素

上使用CSS作爲字符串
idTab5.style.cssText = "overflow:hidden;display:block;height:100%;font-size:40px;line-height:14px;"; 
代替

idTab5.style="overflow:hidden;display:block;height:100%;font-size:12px;line-height:14px;"; 
+0

你一定是在跟我開玩笑,在開始我的代碼正在工作,突然它停止工作..現在改變剛纔你建議再工作...... jeez,非常感謝。 – user1738483

+0

你有什麼想法如何實現一個小小的CSS動畫,因此該div不會變得更快? – user1738483

+0

它http://stackoverflow.com/questions/15768242/trying-to-use-basic-javascript-to-animate-a-div-and-make-it-bigger-but-have-it-g給出了一些上下文給你。 –

0

HTML:

<div id="idTab5" style="height:250px;font-size:12px;line-height:14px; border:1px solid black;"></div> 
     <input id="button" type="button" style="margin-top:5px;font-size:12px;color:black; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar +" onclick="showwMore()"> 
     <input id="button2" type="button" style="display:none;margin-top:5px;display:block;font-size:12px;color:white; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar -" onclick="showwLess()"> 

JAVASCRIPT:

var button = document.getElementById("button"); 
var idTab5 = document.getElementById("idTab5"); 
var button2 = document.getElementById("button2"); 

function showwMore() { 
    button.style.display="none"; 
    idTab5.style="display:block;height:100%;font-size:12px;line-height:14px; border:1px solid black;"; 
     button2.style.display="block"; 

} 

function showwLess() { 
    button2.style.display="none"; 
    idTab5.style="height:250px;font-size:12px;line-height:14px; border:1px solid black;"; 
    button.style.display="block"; 
} 

看到FIDDLE

+0

這不起作用,你不能像你那樣使用多種風格。 'idTab5.style =「高度:250像素;字體大小:12px的;' –