用url和类处理img标签问题
React img tag issue with url and class
我的JSX文件中有以下简单的react代码:
/** @jsx React.DOM */
var Hello = React.createClass({
render: function() {
return <div><img src='http://placehold.it/400x20&text=slide1' alt={event.title} class="img-responsive"/><span>Hello {this.props.name}</span></div>;
}
});
React.renderComponent(<Hello name="World" />, document.body);
DOM中的输出如下:
<div data-reactid=".0">
<img src="http://placehold.it/400x20undefined1" data-reactid=".0.0">
<span data-reactid=".0.1">
<span data-reactid=".0.1.0">Hello </span>
<span data-reactid=".0.1.1">World</span>
</span>
</div>
我有两个问题:
- 图像URL呈现不正确,呈现为"http://placehold.it/400x20undefined1'
- 我图像上的类消失了
有什么想法吗?
请记住,img实际上不是一个DOM元素,而是一个javascript表达式。
-
这是一个JSX属性表达式。在src字符串表达式周围放大括号,它就可以工作了。看见http://facebook.github.io/react/docs/jsx-in-depth.html#attribute-表达式
-
在javascript中,class属性是使用className的引用。请参阅本节中的注释:http://facebook.github.io/react/docs/jsx-in-depth.html#react-复合材料组件
/** @jsx React.DOM */ var Hello = React.createClass({ render: function() { return <div><img src={'http://placehold.it/400x20&text=slide1'} alt="boohoo" className="img-responsive"/><span>Hello {this.props.name}</span></div>; } }); React.renderComponent(<Hello name="World" />, document.body);
var Hello = React.createClass({
render: function() {
return (
<div className="divClass">
<img src={this.props.url} alt={`${this.props.title}'s picture`} className="img-responsive" />
<span>Hello {this.props.name}</span>
</div>
);
}
});
相关文章:
- 我应该如何在 UL 内触发 IMG 标签的点击
- Grails-设置g:点击img标签选择值
- JavaScript on Click 事件在 img 标签上无法正常工作
- 在容器中,我有文本和 img 标签,如何仅使用 js 或 jquery 获取文本
- 如何在单击“提交”按钮后使文本区域和 IMG 标签只读
- javascript 将 IMG 标签中的图像转换为 标签中的实际链接图像
- 当在 img 标签中提到类时,Href 不起作用
- 使用 javascript 从图像数组中更改 img 标签 src.骰子游戏
- 捕获 img 标签并更改图像
- jQuery 按 ID 名称删除 img 标签不起作用
- 在 javascript 中调用 img 标签
- 在 jQuery 中获取 IMG 标签的 ID
- 如何访问嵌套在 h1 标签中的 img 标签元素
- HTML Img 标签使用 Javascript 重新加载
- 在 img 标签上叠加图形边框
- 如何在TR的最后一个TD中添加事件或将ID属性添加到IMG标签
- 安卓:在 img 标签中显示捕获的图像
- 我收到错误.我需要创建 img 标签
- 如何使用jquery添加img标签的alt属性
- 将图像从 img 标签的 src 上传到 php