HTML中允许使用单引号吗?

我是一个在PHP中使用双引号的大用户,这样我可以插入variables而不是连接string。 因此,当我生成HTML时,我经常使用单引号来设置标记字段。 例如:

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

现在这比我更易于阅读

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

要么

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

从简短的search,我听到人们说,HTML字段的单引号不会被每个浏览器识别。 因此,我想知道哪些浏览器会认识到单引号HTML有问题?

这与HTML中单引号变得如此stream行的时间类似? 。 规范中允许使用HTML中的属性的单引号。 我不认为任何浏览器不会理解他们。

正如PhiLho所指出的那样,尽pipe人们普遍认为单引号不能用于属性值,但这种看法是错误的。

XML标准允许属性值的单引号和双引号。

XHTML标准没有说什么改变这个,但是一个相关的部分声明属性值必须用双引号引起来,这可能导致这个混淆。 这个例子只是指出,XHTML中的属性值必须符合XML中属性值的最低标准,这意味着它们必须被引用(与纯粹的HTML不同),但不限制为单个或双引号。

当然,总是有可能遇到一个不符合标准的parsing器,但是当这种情况发生的时候,所有的赌注都无济于事。 所以最好是坚持规格说明。 毕竟,这就是为什么我们有规范。

我听到有人说每个浏览器都无法识别HTML字段的单引号

那个人是错的

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

嗯,我在打字的时候看上面,看到Adam N传播了相同的信念。 如果他可以支持他的肯定,我收回我写的… AFAIK,XML是不可知论的,并接受这两种报价。 我甚至尝试和validation没有问题的XHTML页面只有单引号。

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

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

一样:

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

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

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

我在HTML页面中使用了单引号,并在其中embedded了JavaScript,其工作正常。 testingIE9,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')}; 

最近我遇到了Googlesearch优化的问题。 如果有一个单引号,它似乎没有抓取链接的页面。

…或者只是使用heredocs。 那么你不需要担心转义,除了END

单引号对于HTML来说是不错的,但是它们并没有生成有效的XHTML,如果有人使用仅支持XHTML的浏览器,而不支持HTML,则这可能会产生问题。 我不相信任何这样的浏览器存在,虽然可能有一些用户代理那里需要严格的XHTML。