jQuery ,Vue , React三者遍历数据渲染页面
jQuery, Vue, 和 React 都可以用来遍历数据并渲染页面。以下是每种库的简单示例:
jQuery:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="app">
</div>
<script>
$(document).ready(function(){
var data = ["Item 1", "Item 2", "Item 3"];
$.each(data, function(index, item){
$('#app').append('<p>' + item + '</p>');
});
});
</script>
</body>
</html>
Vue:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue Example</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in items">{{ item }}</li>
</ul>
</div>
<script>
const app = Vue.createApp({
data() {
return {
items: ["Item 1", "Item 2", "Item 3"]
};
}
});
app.mount('#app');
</script>
</body>
</html>
React:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Example</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const { useState } = React;
function App() {
const [items] = useState(["Item 1", "Item 2", "Item 3"]);
return (
<ul>
{items.map(item => <li key={item}>{item}</li>)}
</ul>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>
</body>
</html>
每种库都有其特定的方法来遍历数据和渲染页面。jQuery 使用 $.each()
,Vue 使用 v-for
指令,而 React 使用数组的 map()
函数。每种方法都有其优点和适用场景,开发者可以根据项目需求选择合适的库和方法。
评论已关闭