jQuery object to JavaScript object

前端 未结 5 1012
忘了有多久
忘了有多久 2020-11-30 03:33

If I have a textarea like var txtarea = $(\'textarea\'), how can I set the value to it using the JavaScript property value, and not th

相关标签:
5条回答
  • 2020-11-30 04:23

    The jQuery .get() will do that for you

    http://api.jquery.com/get/

    Quoted:

    Without a parameter, .get() returns all of the elements:

    alert($('li').get());
    

    With an index specified, .get() will retrieve a single element:

    ($('li').get(0));
    

    ...we can use the array dereferencing operator to get at the list item instead:

    alert($('li')[0]);
    
    0 讨论(0)
  • 2020-11-30 04:27

    You can pull the HTMLElement using array notation:

    $('textarea')[0].value
    

    ...will get the value of the first element in the $('textarea') jQuery list.

    0 讨论(0)
  • 2020-11-30 04:29

    jQuery objects are Javascript objects.

    You're trying to learn about the world of pain known as raw DOM scripting.

    You can write

    document.getElementsByTagName("textarea")[0].value = whatever;
    

    This will get the first <textarea> and set its value.
    If you want to set values for all of them, you'll need a loop.

    You can also get a raw DOM element out of a jQuery object by writing $(whatever)[0].

    0 讨论(0)
  • 2020-11-30 04:30

    You can use the dereferencing operator, or the .get() method to "Retrieve the DOM elements matched by the jQuery object."

    Examples:

    txtArea[0].value = "something";
    

    or:

    txtArea.get(0).value = "something";
    
    0 讨论(0)
  • 2020-11-30 04:37

    We can use javascript's querySelector() function to get a standard javascript DOM object.

    Example :

    <ul id="category"><li><label><input data-id="1" data-label="CBSE" data-filterClass="category" name="category" type="checkbox"> CBSE </label></li>
    
    console.log(document.querySelector('input[data-id="1"]'));
    

    You will get javascript DOM object for given selector.

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