w3school include HTML, javascript not working

后端 未结 2 796
谎友^
谎友^ 2021-01-25 03:45

So, I recently used w3school include html, so that i dont have to copy-paste same div to all my pages.

First, i used it for header and footer, which uses some CSS styli

相关标签:
2条回答
  • 2021-01-25 03:52

    You need to delegate your events beacouse w3school script uses ajax and adds new items to the dom

    $('body').on('click','#leftDrop1',function() {
      if ($('#sub1').css('display') == 'block') {
    
        $('#sub1').hide()
    
      } else {
        $("#sub1").show()
      }
    })
    
    $('body').on('click','#leftDrop2',function() {
      if ($('#sub2').css('display') == 'block') {
    
        $('#sub2').hide()
    
      } else {
        $("#sub2").show()
      }
    })
    
    0 讨论(0)
  • 2021-01-25 04:08

    Now w3.includeHTML from w3.js allows a callback after html is loaded.

    w3.includeHTML(function() { 
      $('#leftDrop1').on('click', function() {
        if ($('#sub1').css('display') == 'block') {
          $('#sub1').hide()
        } else {
          $("#sub1").show()
        }
      })
      $('#leftDrop2').on('click', function() {
        if ($('#sub2').css('display') == 'block') {
          $('#sub2').hide()
        } else {
          $("#sub2").show()
        }
      })
    });
    
    0 讨论(0)
提交回复
热议问题