Is it correct to use single quotes for HTML attributes?

前端 未结 15 2556
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 05:57

Recently I\'ve been seeing a lot of this:


    

        
相关标签:
15条回答
  • 2020-12-01 06:44

    Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.

    Example:

    echo "<input type='text' value='$data'/>";
    

    instead of

    echo "<input type=\"text\" value=\"$data\" />";
    

    or

    echo '<input type="text" value="' . $data . '" />';
    

    Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.

    0 讨论(0)
  • 2020-12-01 06:45

    In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :

    echo '<input type="text" value="', $data, '" />';
    
    0 讨论(0)
  • 2020-12-01 06:51

    It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.

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