jquery: get value of custom attribute

前端 未结 2 1838
旧巷少年郎
旧巷少年郎 2020-12-15 15:45

html5 supports the placeholder attribute on input[type=text] elements, but I need to handle non-compliant browsers. I know there are a thousand plugins out ther

相关标签:
2条回答
  • 2020-12-15 16:14

    You can also do this by passing function with onclick event

    <a onclick="getColor(this);" color="red">
    
    <script type="text/javascript">
    
    function getColor(el)
    {
         color = $(el).attr('color');
         alert(color);
    }
    
    </script> 
    
    0 讨论(0)
  • 2020-12-15 16:31

    You need some form of iteration here, as val (except when called with a function) only works on the first element:

    $("input[placeholder]").val($("input[placeholder]").attr("placeholder"));
    

    should be:

    $("input[placeholder]").each( function () {
        $(this).val( $(this).attr("placeholder") );
    });
    

    or

    $("input[placeholder]").val(function() {
        return $(this).attr("placeholder");
    });
    
    0 讨论(0)
提交回复
热议问题