How to get value of a div using javascript

前端 未结 6 2126
情歌与酒
情歌与酒 2020-11-27 21:47

This is my div:

By Color

6条回答
  •  独厮守ぢ
    2020-11-27 22:04

    As I said in the comments, a

    element does not have a value attribute. Although (very) bad, it can be accessed as:

    console.log(document.getElementById('demo').getAttribute);
    

    I suggest using HTML5 data-* attributes rather. Something like this:

    ...

    in which case you could access it using:

    element.getAttribute('data-myValue');
    //Or using jQuery:
    $('#demo').data('myValue');
    

提交回复
热议问题