document.getElementByID is not a function

前端 未结 4 2282
生来不讨喜
生来不讨喜 2021-02-11 13:02

I\'m learning jQuery and was following a tutorial, a very strange error has perplexed me. Here\'s my html :



  
           


        
相关标签:
4条回答
  • 2021-02-11 13:40

    It's getElementById()

    Note the lower-case d in Id.

    0 讨论(0)
  • 2021-02-11 13:41

    In my case, I was using it on an element instead of document, and according to MDN:

    Unlike some other element-lookup methods such as Document.querySelector() and Document.querySelectorAll(), getElementById() is only available as a method of the global document object, and not available as a method on all element objects in the DOM. Because ID values must be unique throughout the entire document, there is no need for "local" versions of the function.

    0 讨论(0)
  • 2021-02-11 13:48

    I've modified your script to work with jQuery, if you wish to do so.

    $(document).ready(function(){
        //To add a task when the user hits the return key
        $('#task-text').keydown(function(evt){
              if(evt.keyCode == 13)
              {
                 add_task($(this), evt);
              }
        });
        //To add a task when the user clicks on the submit button
        $("#add-task").click(function(evt){
            add_task($("#task-text"),evt);
        });
    });
    
    function add_task(textBox, evt){
      evt.preventDefault();
      var taskText = textBox.val();
      $("<li />").text(taskText).appendTo("#tasks");
      textBox.val("");
    };
    
    0 讨论(0)
  • 2021-02-11 14:04

    It's document.getElementById() and not document.getElementByID(). Check the casing for Id.

    0 讨论(0)
提交回复
热议问题