简单的jquery追加不起作用


simple jquery append doesnt work

这里有代码(如果太乱,很抱歉)。我知道这很愚蠢,但我想不通。

协议是根据客户端是否是移动的,向id为movtip的div中添加一些内容(我有自己的类来处理它,所以不用担心):

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>'; 
}

(如果太乱,很抱歉)

这是你的问题。至少你的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) {

打开一个块,但没有右大括号来关闭该块?

简洁易懂的代码可能包含更少的错误;)