HTML允许单引号吗?

时间:2021-11-09 00:21:13

I am a big time user of using double quotes in PHP so that I can interpolate variables rather than concatenating strings. As a result, when I am generating HTML I often use single quotes for setting tag fields. For example:

我很喜欢在PHP中使用双引号,这样我就可以插入变量而不是连接字符串。因此,在生成HTML时,我经常使用单引号来设置标记字段。例如:

$html = "<input type='text' name='address' value='$address'>";

Now this is far more readable to me than either

这对我来说比这两个都更容易读懂

$html = "<input type=\"text\" name=\"address\" value=\"$address\">";

or

$html = '<input type="text" name="address" values="' . $address . '">' ;

From brief searches I have heard people saying that single quotes for HTML fields is not recognized by EVERY browser. Thus I am wondering what browsers would have problems recognizing single quote HTML?

从简短的搜索中,我听到有人说HTML字段的单引号不是每个浏览器都能识别的。因此,我想知道哪些浏览器在识别单引号HTML时会有问题?

10 个解决方案

#1


131  

This is similar to When did single quotes in HTML become so popular?. Single quotes around attributes in HTML are and always have been permitted by the specification. I don't think any browsers wouldn't understand them.

这与HTML中的单引号什么时候变得如此流行很相似。HTML中关于属性的单引号一直是规范所允许的。我认为任何浏览器都不会理解它们。

#2


45  

As noted by PhiLho, although there is a widely spread belief that single quotes are not allowed for attribute values, that belief is wrong.

正如PhiLho所指出的,尽管普遍认为属性值不允许使用单引号,但这种观点是错误的。

The XML standard permits both single and double quotes around attribute values.

XML标准允许对属性值使用单引号和双引号。

The XHTML standard doesn't say anything to change this, but a related section which states that attribute values must be quoted uses double quotes in the example, which has probably lead to this confusion. This example is merely pointing out that attribute values in XHTML must meet the minimum standard for attribute values in XML, which means they must be quoted (as opposed to plain HTML which doesn't care), but does not restrict you to either single or double quotes.

XHTML标准并没有说什么要改变这一点,但是有一个相关的部分规定必须引用属性值,在这个例子中使用了双引号,这可能会导致这种混淆。这个例子只是指出XHTML中的属性值必须满足XML中属性值的最小标准,这意味着它们必须被引用(与不关心的普通HTML相反),但不限制您使用单引号或双引号。

Of course, it's always possible that you'll encounter a parser which isn't standards-compliant, but when that happens all bets are off anyway. So it's best to just stick to what the specification says. That's why we have specifications, after all.

当然,您可能会遇到不符合标准的解析器,但是当这种情况发生时,所有的赌注都会落空。所以最好还是按照规范说的去做。这就是我们有规格说明的原因。

#3


13  

I have heard people saying that single quotes for HTML fields is not recognized by EVERY browser

我听说过有人说HTML字段的单引号不是每个浏览器都能识别的

That person is wrong.

那个人是错误的。

#4


5  

Don't believe everything you see on Internet...
Funnily, I just answered something similar to somebody declaring single quotes are not valid in XHTML...

不要相信你在网上看到的一切……有趣的是,我刚刚回答了一些类似于某人声明单引号在XHTML中无效的问题……

Mmm, I look above while typing, and see that Adam N propagates the same belief. If he can back up his affirmation, I retract what I wrote... AFAIK, XML is agnostic and accepts both kinds of quote. I even tried and validated without problem an XHTML page with only single quotes.

嗯,我一边打字一边看上面,发现亚当N传播了同样的信念。如果他能支持他的肯定,我收回我写的……AFAIK, XML是不可知论者,接受这两种报价。我甚至尝试并验证了一个只有单引号的XHTML页面。

#5


5  

Only problem is data going into TEXT INPUT fields. Consider

唯一的问题是数据进入文本输入字段。考虑

<input value='it's gonna break'/>

Same with:

相同:

<input value="i say - "this is gonna be trouble" "/>

You can't escape that, you have to use htmlspecialchars.

你不能逃避,你必须使用htmlspecialchars。

#6


2  

I also tend to use single quotes in HTML and I have never experienced a problem.

我也倾向于在HTML中使用单引号,我从来没有遇到过问题。

#7


1  

I used single quotes in HTML pages and embedded JavaScripts into it and its works fine. Tested in IE9, Chrome and Firefox - seems working fine.

我在HTML页面中使用了单引号,并将javascript嵌入其中。在IE9、Chrome和Firefox中测试,效果似乎还不错。

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
        <title>Bethanie Inc. data : geographically linked</title>
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
        <script src='https://maps.googleapis.com/maps/api/js?v=3.11&sensor=false' type='text/javascript'></script>
        <script type='text/javascript'> 
        // check DOM Ready
        $(document).ready(function() {
            // execute
            (function() {
                /////////////// Addresses ///////////////////
                var locations = new Array();
                var i = 0;
                locations[i++] = 'L,Riversea: Comp Site1 at Riversea,1 Wallace Lane Mosman Park WA 6012'
                locations[i++] = 'L,Wearne: Comp Site2 at Wearne,1 Gibney St Cottesloe WA 6011'
                locations[i++] = 'L,Beachside:Comp Site3 Beachside,629 Two Rocks Rd Yanchep WA 6035'

                /////// Addresses/////////
                var total_locations = i;
                i = 0;
                console.log('About to look up ' + total_locations + ' locations');
                // map options
                var options = {
                    zoom: 10,
                    center: new google.maps.LatLng(-31.982484, 115.789329),//Bethanie  
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: true
                };
                // init map
                console.log('Initialise map...');
                var map = new google.maps.Map(document.getElementById('map_canvas'), options);
               // use the Google API to translate addresses to GPS coordinates 
               //(See Limits: https://developers.google.com/maps/documentation/geocoding/#Limits)
                var geocoder = new google.maps.Geocoder();
                if (geocoder) {
                    console.log('Got a new instance of Google Geocoder object');
                    // Call function 'createNextMarker' every second
                    var myVar = window.setInterval(function(){createNextMarker()}, 700);
                    function createNextMarker() {
                        if (i < locations.length) 
                       {
                            var customer = locations[i];
                            var parts = customer.split(','); // split line into parts (fields)
                            var type= parts.splice(0,1);    // type from location line (remove)
                            var name = parts.splice(0,1);    // name from location line(remove)
                            var address =parts.join(',');   // combine remaining parts
                            console.log('Looking up ' + name + ' at address ' + address);
                            geocoder.geocode({ 'address': address }, makeCallback(name, type));
                            i++; // next location in list
                            updateProgressBar(i / total_locations);


                        } else 
                       {
                            console.log('Ready looking up ' + i + ' addresses');
                            window.clearInterval(myVar);
                        }
                    }

                    function makeCallback(name,type) 
                   {
                        var geocodeCallBack = function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {
                                var longitude = results[0].geometry.location.lng();
                                var latitude = results[0].geometry.location.lat();
                                console.log('Received result: lat:' + latitude + ' long:' + longitude);
                                var marker = new google.maps.Marker({
                                    position: new google.maps.LatLng(latitude, longitude),
                                    map: map,
                                    title: name + ' : ' + '\r\n' + results[0].formatted_address});// this is display in tool tip/ icon color
                                   if (type=='E')  {marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')};

#8


0  

Recently i've experienced a problem with Google Search optimization. If has a single quotes, it doesn't seem to crawl linked pages.

最近我遇到了谷歌搜索优化的问题。如果只有一个引号,它似乎不会抓取链接页面。

#9


-1  

... or just use heredocs. Then you don't need to worry about escaping anything but END.

…或者只是使用heredoc。那你就不用担心会有什么事情发生了。

#10


-8  

Single Quotes are fine for HTML, but they don't make valid XHTML, which might be problematic if anybody was using a browser which supported only XHTML, but not HTML. I don't believe any such browsers exist, though there are probably some User-Agents out there that do require strict XHTML.

单引号对于HTML来说是可以的,但是它们不能成为有效的XHTML,如果有人使用的浏览器只支持XHTML,而不支持HTML,那么这可能会有问题。我不相信存在这样的浏览器,尽管可能有一些用户代理需要严格的XHTML。

#1


131  

This is similar to When did single quotes in HTML become so popular?. Single quotes around attributes in HTML are and always have been permitted by the specification. I don't think any browsers wouldn't understand them.

这与HTML中的单引号什么时候变得如此流行很相似。HTML中关于属性的单引号一直是规范所允许的。我认为任何浏览器都不会理解它们。

#2


45  

As noted by PhiLho, although there is a widely spread belief that single quotes are not allowed for attribute values, that belief is wrong.

正如PhiLho所指出的,尽管普遍认为属性值不允许使用单引号,但这种观点是错误的。

The XML standard permits both single and double quotes around attribute values.

XML标准允许对属性值使用单引号和双引号。

The XHTML standard doesn't say anything to change this, but a related section which states that attribute values must be quoted uses double quotes in the example, which has probably lead to this confusion. This example is merely pointing out that attribute values in XHTML must meet the minimum standard for attribute values in XML, which means they must be quoted (as opposed to plain HTML which doesn't care), but does not restrict you to either single or double quotes.

XHTML标准并没有说什么要改变这一点,但是有一个相关的部分规定必须引用属性值,在这个例子中使用了双引号,这可能会导致这种混淆。这个例子只是指出XHTML中的属性值必须满足XML中属性值的最小标准,这意味着它们必须被引用(与不关心的普通HTML相反),但不限制您使用单引号或双引号。

Of course, it's always possible that you'll encounter a parser which isn't standards-compliant, but when that happens all bets are off anyway. So it's best to just stick to what the specification says. That's why we have specifications, after all.

当然,您可能会遇到不符合标准的解析器,但是当这种情况发生时,所有的赌注都会落空。所以最好还是按照规范说的去做。这就是我们有规格说明的原因。

#3


13  

I have heard people saying that single quotes for HTML fields is not recognized by EVERY browser

我听说过有人说HTML字段的单引号不是每个浏览器都能识别的

That person is wrong.

那个人是错误的。

#4


5  

Don't believe everything you see on Internet...
Funnily, I just answered something similar to somebody declaring single quotes are not valid in XHTML...

不要相信你在网上看到的一切……有趣的是,我刚刚回答了一些类似于某人声明单引号在XHTML中无效的问题……

Mmm, I look above while typing, and see that Adam N propagates the same belief. If he can back up his affirmation, I retract what I wrote... AFAIK, XML is agnostic and accepts both kinds of quote. I even tried and validated without problem an XHTML page with only single quotes.

嗯,我一边打字一边看上面,发现亚当N传播了同样的信念。如果他能支持他的肯定,我收回我写的……AFAIK, XML是不可知论者,接受这两种报价。我甚至尝试并验证了一个只有单引号的XHTML页面。

#5


5  

Only problem is data going into TEXT INPUT fields. Consider

唯一的问题是数据进入文本输入字段。考虑

<input value='it's gonna break'/>

Same with:

相同:

<input value="i say - "this is gonna be trouble" "/>

You can't escape that, you have to use htmlspecialchars.

你不能逃避,你必须使用htmlspecialchars。

#6


2  

I also tend to use single quotes in HTML and I have never experienced a problem.

我也倾向于在HTML中使用单引号,我从来没有遇到过问题。

#7


1  

I used single quotes in HTML pages and embedded JavaScripts into it and its works fine. Tested in IE9, Chrome and Firefox - seems working fine.

我在HTML页面中使用了单引号,并将javascript嵌入其中。在IE9、Chrome和Firefox中测试,效果似乎还不错。

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
        <title>Bethanie Inc. data : geographically linked</title>
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
        <script src='https://maps.googleapis.com/maps/api/js?v=3.11&sensor=false' type='text/javascript'></script>
        <script type='text/javascript'> 
        // check DOM Ready
        $(document).ready(function() {
            // execute
            (function() {
                /////////////// Addresses ///////////////////
                var locations = new Array();
                var i = 0;
                locations[i++] = 'L,Riversea: Comp Site1 at Riversea,1 Wallace Lane Mosman Park WA 6012'
                locations[i++] = 'L,Wearne: Comp Site2 at Wearne,1 Gibney St Cottesloe WA 6011'
                locations[i++] = 'L,Beachside:Comp Site3 Beachside,629 Two Rocks Rd Yanchep WA 6035'

                /////// Addresses/////////
                var total_locations = i;
                i = 0;
                console.log('About to look up ' + total_locations + ' locations');
                // map options
                var options = {
                    zoom: 10,
                    center: new google.maps.LatLng(-31.982484, 115.789329),//Bethanie  
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: true
                };
                // init map
                console.log('Initialise map...');
                var map = new google.maps.Map(document.getElementById('map_canvas'), options);
               // use the Google API to translate addresses to GPS coordinates 
               //(See Limits: https://developers.google.com/maps/documentation/geocoding/#Limits)
                var geocoder = new google.maps.Geocoder();
                if (geocoder) {
                    console.log('Got a new instance of Google Geocoder object');
                    // Call function 'createNextMarker' every second
                    var myVar = window.setInterval(function(){createNextMarker()}, 700);
                    function createNextMarker() {
                        if (i < locations.length) 
                       {
                            var customer = locations[i];
                            var parts = customer.split(','); // split line into parts (fields)
                            var type= parts.splice(0,1);    // type from location line (remove)
                            var name = parts.splice(0,1);    // name from location line(remove)
                            var address =parts.join(',');   // combine remaining parts
                            console.log('Looking up ' + name + ' at address ' + address);
                            geocoder.geocode({ 'address': address }, makeCallback(name, type));
                            i++; // next location in list
                            updateProgressBar(i / total_locations);


                        } else 
                       {
                            console.log('Ready looking up ' + i + ' addresses');
                            window.clearInterval(myVar);
                        }
                    }

                    function makeCallback(name,type) 
                   {
                        var geocodeCallBack = function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {
                                var longitude = results[0].geometry.location.lng();
                                var latitude = results[0].geometry.location.lat();
                                console.log('Received result: lat:' + latitude + ' long:' + longitude);
                                var marker = new google.maps.Marker({
                                    position: new google.maps.LatLng(latitude, longitude),
                                    map: map,
                                    title: name + ' : ' + '\r\n' + results[0].formatted_address});// this is display in tool tip/ icon color
                                   if (type=='E')  {marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')};

#8


0  

Recently i've experienced a problem with Google Search optimization. If has a single quotes, it doesn't seem to crawl linked pages.

最近我遇到了谷歌搜索优化的问题。如果只有一个引号,它似乎不会抓取链接页面。

#9


-1  

... or just use heredocs. Then you don't need to worry about escaping anything but END.

…或者只是使用heredoc。那你就不用担心会有什么事情发生了。

#10


-8  

Single Quotes are fine for HTML, but they don't make valid XHTML, which might be problematic if anybody was using a browser which supported only XHTML, but not HTML. I don't believe any such browsers exist, though there are probably some User-Agents out there that do require strict XHTML.

单引号对于HTML来说是可以的,但是它们不能成为有效的XHTML,如果有人使用的浏览器只支持XHTML,而不支持HTML,那么这可能会有问题。我不相信存在这样的浏览器,尽管可能有一些用户代理需要严格的XHTML。