简体   繁体   English

如何在 React 中禁用图像

[英]How to Disable Images in React

I am using a set if images in a row.我正在使用一组连续的图像。 There is a text box input above these images and based on the input i need to enable/disable images?这些图像上方有一个文本框输入,我需要根据输入启用/禁用图像吗? How to do this in React.如何在 React 中执行此操作。

I tried adding "disable" to image tag and disabled=true but both didn't work.我尝试将“disable”添加到图像标签和 disabled=true 但两者都不起作用。

const IBox = props => 
  <div  style={props.styles.imageContainer} >
    <img id="image1" src={require('../../../public/images/image1.jpg')} alt = "Image1" /><span >&nbsp;</span>
    <img id="image2" src={require('../../../public/images/image2.jpg')} alt ="Image2" /><span>&nbsp;</span>
    <img id="image3" src={require('../../../public/images/image3.jpg')} alt ="Image3" /><span>&nbsp;</span>
    <img id="image4" src={require('../../../public/images/image4.jpg')} alt ="Image4"/>
  </div>
export default IBox;

There is no such thing as "disabling" images. 没有“禁用”图像这样的东西。 You can only disable form elements, as they are the only interactive html elements. 您只能禁用表单元素,因为它们是唯一的交互式html元素。 See w3c specification to see which items can be disabled exactly. 请参阅w3c 规范以查看可以准确禁用的项目。

That is unless you write your own custom definition for disabling images. 除非您编写自己的自定义定义以禁用图像。 For example, you could add a class disabled to the image, and style that class to be greyed out, using CSS. 例如,您可以使用CSS向图像添加disabled的类,并将该类设置为灰色。

You could also combine CSS with WebComponents to have an element that with a disabled attribute. 您还可以将CSS与WebComponents结合使用,以获得具有禁用属性的元素。 You could style its disabled style. 您可以设置其禁用的样式。

See also docs for the <img /> tag. 另请参阅<img />标记的文档

If you mean hide/show.. you simply may use state to disable your image ie 如果你的意思是隐藏/显示..你只需使用状态来禁用你的图像,即

{this.state.img1 && <img id="image1" src={require('../../../public/images/image1.jpg')} alt = "Image1" />}

if by disable you mean to make it like grey, low opacity,etc... you may use an state : 如果禁用你的意思是让它像灰色,低不透明度等...你可以使用一个状态:

style= this.state.disable? style = this.state.disable? {{style_disable}}: {{style_enable}} {{style_disable}}:{{style_enable}}

Use <input /> instead of <img /> as in this example.在这个例子中使用<input />而不是<img /> You can provide the same functionality with the type="image" attribute.您可以使用type="image"属性提供相同的功能。 This way you can use the disabled attribute.这样你就可以使用disabled属性了。

<input
  type="image"
  disabled={disabled}
/>

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

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