当前位置: 首页> 健康> 养生 > 简单个人网页制作_网页游戏都有哪些_汕头疫情最新消息_北京seo课程培训

简单个人网页制作_网页游戏都有哪些_汕头疫情最新消息_北京seo课程培训

时间:2025/8/23 9:14:12来源:https://blog.csdn.net/weixin_43159039/article/details/143199351 浏览次数:0次
简单个人网页制作_网页游戏都有哪些_汕头疫情最新消息_北京seo课程培训

jQuery快速填充非form数据

先看看jQuery根据name填充form表单数据

<!DOCTYPE html>
<html><head><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head><body><form id="myForm"><input type="text" name="username" /><input type="email" name="email" /><input type="password" name="password" /></form><script>// 模拟数据const formData = {username: 'JohnDoe',email: 'johndoe@example.com',password: 'secretpassword'};// 填充表单$('#myForm input').each(function () {const inputName = $(this).attr('name');if (formData[inputName]) {$(this).val(formData[inputName]);}});</script></body></html>

按照相同的思路,渲染<span>

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head><body><table><tr><td class="tbox03tdr" width="20%"><span prop="salesOrgName"></span></td><td class="tbox03tdr" width="20%"><span prop="clientName"></span></td></tr></table><script>// 发起 Ajax 请求$.ajax({url: 'yourDataUrl', // 替换为实际的后端数据接口 URL,这里假设为给定的 JSON 数据的模拟 URLmethod: 'GET',success: function (data) {// 获取具有 prop 属性的 span 元素const spans = $('span[prop]');// 遍历 span 元素并设置内容spans.each(function () {const propValue = $(this).attr('prop');$(this).html(data[propValue]);});},error: function (error) {console.error('Ajax 请求失败:', error);}});</script>
</body></html>

通过这样的方式,只要将span中定义prop属性和json中的key保持一致,即可填充数据

关键字:简单个人网页制作_网页游戏都有哪些_汕头疫情最新消息_北京seo课程培训

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: