02_表单空间绑定
02_表单空间绑定
AllenOR灵感 发表于5个月前
02_表单空间绑定
  • 发表于 5个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

数据双向绑定(input即时展示)

Vue.js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>
</head>
<body>

<div id="demo">
    <p>{{ message }}</p>
    <input type="text" v-model="message">
</div>

<script>
var demo = new Vue({
    el: '#demo',
    data: {
        message: 'Hello Vue.js!'
    }
})
</script>
</body>
</html>

ReactJS 不采用ES6、不采用JSX

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="root"></div>

<script src="react.js"></script>
<script src="react-dom.js"></script>
<script src="remarkable.js"></script>
<script>
var MarkdownEditor = React.createClass({

    getInitialState: function() {
        return {value: 'Type some *markdown* here!'}
    },

    handleChange: function(e) {
        this.setState({value: e.target.value});
    },

    getRawMarkup: function() {
        var md = new Remarkable();
        return { __html: md.render(this.state.value) };
    },

    render: function () {
        return (
            React.createElement(
                'div',
                {className: 'MarkdownEditor'},
                React.createElement('h3', null, 'Input'),
                React.createElement(
                    'textarea',
                    {type: 'text',
                     onChange: this.handleChange,
                     defaultValue: this.state.value,
                     rows: '8'}
                ),
                React.createElement('h3', null, 'Output'),
                React.createElement(
                    'div',
                    {className: 'content',
                    dangerouslySetInnerHTML: this.getRawMarkup()})
            )
        )
    }

});

ReactDOM.render(
    React.createElement(MarkdownEditor),
    document.getElementById('root')
);
</script>
</body>
</html>

ReactJS 采用ES6 不采用JSX

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="root"></div>

<script src="babel.js"></script>
<script src="react.js"></script>
<script src="react-dom.js"></script>
<script src="remarkable.js"></script>
<script type="text/babel">
class MarkdownEditor extends React.Component {

    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = { value: 'Type some *markdown* here!' };
    }

    handleChange(e) {
        this.setState({ value: e.target.value });
    }

    getRawMarkup() {
        var md = new Remarkable();
        return { __html: md.render(this.state.value) };
    }

    render() {
        return React.createElement(
            "div",
            { className: "MarkdownEditor" },
            React.createElement("h3", null, "Input"),
            React.createElement(
                "textarea",
                {onChange: this.handleChange,
                 defaultValue: this.state.value,
                 rows: '8'}
            ),
            React.createElement("h3", null, "Output"),
            React.createElement(
                "div",
                {className: "content",
                dangerouslySetInnerHTML: this.getRawMarkup()}
            )
        );
    }
}

ReactDOM.render(
    React.createElement(MarkdownEditor, null),
    document.getElementById('root')
);
</script>
</body>
</html>

ReactJS 采用ES6 采用JSX

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="root"></div>

<script src="babel.js"></script>
<script src="react.js"></script>
<script src="react-dom.js"></script>
<script src="remarkable.js"></script>
<script type="text/babel">
class MarkdownEditor extends React.Component {

    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {value: 'Type some *markdown* here!'};
    }

    handleChange(e) {
        this.setState({value: e.target.value});
    }

    getRawMarkup() {
        var md = new Remarkable();
        return { __html: md.render(this.state.value) };
    }

    render() {
        return (
            <div className="MarkdownEditor">
            <h3>Input</h3>
            <textarea onChange={this.handleChange}
                       defaultValue={this.state.value} />
            <h3>Output</h3>
            <div className="content"
                 dangerouslySetInnerHTML={this.getRawMarkup()} />
            </div>
        );
    }
}

ReactDOM.render(
    <MarkdownEditor />,
    document.getElementById('root')
);
</script>
</body>
</html>
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 6
博文 2139
码字总数 82983
×
AllenOR灵感
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: