2014-09-26 71 views
35

我有以下簡單的反應代碼在我的JSX文件:陣營img標籤問題與URL和

/** @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> 

我有兩個問題與它:

任何想法?

回答

50

請記住,您的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); 
    
+2

如果你檢查輸出,可以實現的alt屬性也不翼而飛。 :( – ruff 2015-10-20 08:45:52

+1

該alt應該像'alt = {「boohoo」}'。 – 2016-11-10 15:54:10

0
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> 
    ); 
    } 
});