栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用jQuery解决动态加载页面中的重复对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用jQuery解决动态加载页面中的重复对象?

以下函数将处理部分加载视图页面中的数据,并为脚本中的每个jQuery选择器添加指定的上下文。这个答案很好用。但是,它不支持外部脚本文件。

function renderPartialView(control, data){    // For detecting all script tag in loaded data.    var reExtractscript = /(<script type="text/javascript">)([sS]+?)(</script>)/gi;    // For detecting all "$" sign (must be jQuery object) in loaded data.    var reFindDollarSign = /$(([S]+?))/gi;    // Find all matched string in loaded data.    var result = reExtractscript.exec(data);    var allscript = '';    if (result)    {        for (i = 0; i < result.length; i += 4)        {    // Remove current script from loaded script.         data = data.replace(result[i], ''); // Replace all "$" function by adding context parameter that is control. allscript += result[i+2].replace(reFindDollarSign, '$($1, "' + control + '")');        }    }    // Load non-script html to control.    $(control).html(data);    // evaluate all script that is found in loaded data.    eval(allscript);}// This script will partially download view page from server in the same domain$(function(){    $.get(getUrl('~/Profile/Section/ViewEducation'), null, function(data)    {        // When partial loading is complete, all loaded data will be sent to “renderPartialView” function        renderPartialView('#education-view', data);    });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382126.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号