小编典典

用URL和类反应img标签问题

reactjs

我的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>

我有两个问题:

有任何想法吗?


阅读 304

收藏
2020-07-22

共1个答案

小编典典

请记住,您的img实际上不是DOM元素,而是JavaScript表达式。

  1. 这是一个JSX属性表达式。将大括号放在src字符串表达式周围,它将起作用。参见http://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions

  2. 在javascript中,class属性是使用className的引用。请参阅本节中的注释:http : //facebook.github.io/react/docs/jsx-in-depth.html#react-composite-components

        /** @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);
2020-07-22