热门关键字:
jquery > jquery教程 > jquery教程 > 使用jQuery在textarea中键入时自动更新div内容

使用jQuery在textarea中键入时自动更新div内容

291
作者:管理员
发布时间:2021/2/6 14:27:48
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3912
您可以将jQuerykeyup()方法与val()和text()方法结合使用,以同时更新<div>元素内容,同时用户在中写入文本<textarea>。


让我们尝试以下示例以了解其基本工作原理:


例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Auto Update Div with Content from Textarea</title>
<style>
    .output{
        padding: 10px;
        min-height: 50px;
        border: 1px solid #e4e4e4;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("#myTextarea").keyup(function(){
        // Getting the current value of textarea
        var currentText = $(this).val();
        
        // Setting the Div content
        $(".output").text(currentText);
    });
});
</script>
</head>
<body>
    <form>
        <textarea id="myTextarea" rows="5" cols="60" placeholder="Type something here..."></textarea>
    </form>
    <div class="output"></div>
</body>
</html>




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jQuery
友荐云推荐