简体   繁体   English

将属性声明为 false 的正确方法是什么?

[英]What's the proper way to declare an attribute to be false?

In HTML5, you can add the attribute draggable to any element -- it works like checked ;在 HTML5 中,您可以将属性draggable添加到任何元素 - 它的工作方式类似于checked it doesn't require a value.它不需要一个值。 If you do want to specify a value (eg to make it valid XML), I think the official standard syntax is checked="checked" , but other things work too (including checked="" and checked="false" , confusingly).如果您确实想指定一个值(例如,使其成为有效的 XML),我认为官方标准语法是checked="checked" ,但其他东西也可以(包括checked=""checked="false" ,令人困惑) . The standard way to make it 'false' is not to include the attribute at all.使其为“假”的标准方法是根本不包含该属性。

<img> elements seem to be 'draggable' by default (even if you don't include a draggable attribute) – that's why you get the 'ghost' image effect when you drag regular images in web pages.默认情况下, <img>元素似乎是“可拖动的”(即使您不包含draggable属性)——这就是为什么当您在 web 页面中拖动常规图像时会获得“幽灵”图像效果的原因。 You get this same effect on non- img elements if you mark them draggable .如果将非img元素标记为draggable ,您将获得同样的效果。

So how do you disable it on img elements?那么如何在img元素上禁用它呢? You can set the property to false in JavaScript.您可以在 JavaScript 中将该属性设置为 false。 But what about in the HTML?但是在 HTML 中呢?

(From experimenting, I know that draggable="false" seems to work in Chrome – is that a standard syntax that works everywhere? If so, why doesn't checked="false" work the same way?) (通过实验,我知道draggable="false"似乎可以在 Chrome 中使用——这是一种在任何地方都可以使用的标准语法吗?如果是这样,为什么checked="false"不能以同样的方式工作?)

Well, W3C says you're right;好吧, W3C 说你是对的; "true", "false" and "auto" should work (not to say it will everywhere). “true”、“false”和“auto”应该可以工作(并不是说它无处不在)。

Edit: http://jsfiddle.net/nwellcome/DRSbc/ for to experiment with in different browsers.编辑: http://jsfiddle.net/nwellcome/DRSbc/用于在不同的浏览器中进行试验。

Take a look here (scroll to 'Global Attributes' and click draggable ).看看这里(滚动到“全局属性”并单击draggable )。

All HTML elements may have the draggable content attribute set.所有 HTML 元素都可以设置可拖动内容属性。 The draggable attribute is an enumerated attribute.可拖动属性是一个枚举属性。 It has three states.它具有三个状态。 The first state is true and it has the keyword true.第一个 state 是 true,它有关键字 true。 The second state is false and it has the keyword false.第二个 state 是 false,它有关键字 false。 The third state is auto;第三个 state 是自动的; it has no keywords but it is the missing value default.它没有关键字,但它是默认的缺失值。

The true state means the element is draggable;真正的 state 意味着元素是可拖动的; the false state means that it is not.错误的 state 意味着它不是。 The auto state uses the default behavior of the user agent.自动 state 使用用户代理的默认行为。

So the answer is: to disable the draggable attribute for a HTML5 element, use draggable="false"所以答案是:要禁用 HTML5 元素的可拖动属性,请使用draggable="false"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM