2014-04-27 62 views
0

有代碼(對不起,如果太亂)。我知道這是愚蠢的東西,但我不知道。簡單的jquery append不工作

的交易是添加一些內容到該分區與依靠,如果客戶端是移動還是不ID movtip(我有自己的類來處理它,所以不要擔心):

if ($deviceType !== telephone && $deviceType !== tablet) { 

    echo ' 
<div class="alert alert-warning alert-dismissable" id="movtip"></div> 
<script type="text/javascript"> 
    if (getCookie(\'hideTip\') != 1) { 
    $(\'#movtip\').append(\'<button type="button" class="close" data-dismiss="alert" aria- 
hidden="true" onclick="hideTip();">&times;</button>blah blah\'); 
</script>'; 

} else { 

    echo ' 
<script type="text/javascript"> 
    if (getCookie(\'hideTip\') != 1) { 
    $(\'#movtip\').append(\'<div class="alert alert-warning alert-dismissable"> 
     <button type="button" class="close" data-dismiss="alert" aria-hidden="true"   onclick="hideTip();">&times;</button> 
blah blah</div>\'); 
</script>'; 

} 
+1

你能解釋什麼不起作用嗎? –

+0

代碼看起來不錯,你的cookie可能有問題「hideTip」,也許getCookie('hideTip')實際上返回1? –

+0

你有代碼錯誤。 }錯過了。您可以使用https://getfirebug.com/來調試Javascript代碼。 – Huseyin

回答

1

(對不起如果太亂)

這個你的問題。至少,有一個在你的JavaScript,但因爲你是如何格式化你的代碼(並選擇呼應它,而不是使用PHP塊語法)語法錯誤幾乎不可能被發現:

<?php if ($deviceType !== telephone && $deviceType !== tablet) : ?> 
    <div class="alert alert-warning alert-dismissable" id="movtip"></div> 
    <script type="text/javascript"> 
     if (getCookie('hideTip') != 1) { 
      $('#movtip').append('<button type="button" class="close" data-dismiss="alert" aria-hidden="true" onclick="hideTip();">&times;</button>blah blah'); 
    </script> 

<?php else: ?> 

    <script type="text/javascript"> 
     if (getCookie('hideTip') != 1) { 
       $('#movtip').append('<div class="alert alert-warning alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true" onclick="hideTip();">&times;</button>blah blah</div>') 
    </script> 

<?php endif; ?> 

注意

if (getCookie('hideTip') != 1) { 

打開一個塊,但沒有關閉大括號關閉該塊?

很容易理解的代碼很可能包含更少的錯誤代碼;)