场景:
请提出建议。
谢谢。
这是当前的代码:
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class NameForm extends React.Component {
render() {
return (<a href="asdf.txt" onclick="$('a').hide()">this is a link</a>)
}
}
ReactDOM.render(
<NameForm />,
document.getElementById('root')
);
</script>
</body>
新代码:
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class NameForm extends React.Component {
render() {
let name = "asdf" // this would be a more complicated proccess
return (<a href=name+".txt" onclick="$('a').hide()">this is a link</a>)
}
}
ReactDOM.render(
<NameForm />,
document.getElementById('root')
);
</script>
</body>
答案 0 :(得分:6)
在反应中,没有HTML,只有JSX。请在{}
之间将JSX中的所有变量用法括起来。
请尝试以下操作:
return <a href={`${name}.txt`} onclick="$('a').hide()">this is a link</a>