简体   繁体   中英

simple jquery append doesnt work

There's code (sorry if its too messy). I know it's stupid and stuff but i cant figure it out.

The deal is to add some content into that div with id movtip depending on if client is mobile or not (i got own class to deal with it so dont worry):

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

}

(sorry if its too messy)

This is your problem. At the very least there's a syntax error in your JavaScript but because of how you've formatted your code (and chosen to echo it rather than using block syntax in PHP) it's almost impossible to spot:

<?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; ?>

Notice that

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

Opens a block but there's no closing brace to close that block?

Neat code that's easy to understand is code which is likely to contain fewer bugs ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM