用url和类来反应img标记问题

Ser*_*ver 39 javascript css reactjs

我的JSX文件中有以下简单的反应代码:

/** @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);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

有任何想法吗?

小智 61

请记住,你的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);
    
    Run Code Online (Sandbox Code Playgroud)

  • 如果检查输出,您可以意识到alt属性也丢失了.:( (2认同)
  • alt 应该像 `alt={"boohoo"}`。 (2认同)