附加ajax内容并在其中执行脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了附加ajax内容并在其中执行脚本相关的知识,希望对你有一定的参考价值。
/** * attach_and_execute_scripts helper function * Copyright (c) 2010 by Anders Ytterström <http://madr.se> * Released under the MIT license. * * Description: * A light-weight function to extract and execute SCRIPT from * an ajax response. it also attach the new content to the DOM * tree. */ function attach_and_execute_scripts(wrapper_elm, ajax_response, create_target) { var target_elm; var extracted_scripts; extracted_scripts = (function (tmp_elm) { tmp_elm.innerhtml = ajax_response; var refs = [], scr = tmp_elm.getElementsByTagName('script'); for (var i = 0, max = scr.length; i < max; i++) { refs.push(scr[i].cloneNode(true)); } return refs; })(document.createElement('div')); // will prevent Gecko from executing scripts twice cleaned_markup = ajax_response .replace(/<script[^>]*>[nsS]*?</script>/img, ''); target_elm = create_target(cleaned_markup); wrapper_elm.appendChild(target_elm); // caution: document.write will break the page! if (extracted_scripts) { for(var i = 0, max = extracted_scripts.length; i < max; i++) { target_elm.appendChild(extracted_scripts[i]); } } }
以上是关于附加ajax内容并在其中执行脚本的主要内容,如果未能解决你的问题,请参考以下文章