简体   繁体   English

document.write()和Ajax-不起作用,正在寻找替代方法

[英]document.write() and Ajax - Doesn't work, looking for an alternative

I recently asked a question here , and received a great response ( which I will shortly be accepting the most active answer of, barring better alternatives arise ) but unfortunately it seems the of the two options suggested, neither will be compatible with Ajax ( or any dynamically added content that includes such "inline-relative jQuery" ) 我最近在这里提出了一个问题,并得到了很好的答复( 不久我将接受最积极的回答,除非出现更好的替代方案 ),但不幸的是,似乎建议的两个选项中的两个都不与Ajax兼容( 或动态添加的内容,其中包括此类“内联jQuery”

Anyways, my question pertains to good ole' document.write() . 无论如何,我的问题与“ ole'document.write document.write()

While a page is still rendering, it works great; 尽管页面仍在渲染,但效果很好。 not so much when an appended snippet contains it. 附加的片段包含该片段时,它的作用不大。 Are there any alternatives that won't destroy the existing page content, yet still append a string inline, as in where the call is occurring? 是否有其他选择不会破坏现有页面内容,但仍会像在进行调用的位置那样内联附加字符串?

In other words, is there a way/alternative to document.write() that when called post-render, doesn't destroy existing page content? 换句话说,是否有一种方法/替代document.write()在调用后期渲染时不会破坏现有页面内容? An Ajax friendly version so to speak? 可以说是Ajax友好版本吗?


This is where I'm going: 这是我要去的地方:

var _inline_relative_index = 0;
function $_inlineRelative(){
    // i hate non-dedicated string concatenation operators
    var inline_relative_id = ('_inline_relative_{index}').replace('{index}', (++_inline_relative_index).toString());
    document.write(('<br id="{id}" />').replace('{id}', inline_relative_id));
    return $(document.getElementById(inline_relative_id)).remove().prev('script');
}

And then: 接着:

<div>
    <script type="text/javascript">
        (function($script){
            // the container <div> background is now red.
            $script.parent().css({ 'background-color': '#f00' });
        })($_inlineRelative());
    </script>
</div>

you have access to the innerHTML property of each DOM node. 您可以访问每个DOM节点的innerHTML属性。 If you set it straight out you might destroy elements, but if you append more HTML to it, it'll preserve the existing HTML. 如果将其设置得直截了当,则可能会破坏元素,但是如果向其添加更多HTML,它将保留现有的HTML。

document.body.innerHTML += '<div id="foo">bar baz</div>';

There are all sorts of nuances to the sledgehammer that is innerHTML , so I highly recommend using a library such as jQuery to normalize everything for you. innerHTML对大锤有种种细微差别,因此我强烈建议您使用jQuery之类的库为您标准化所有内容。

You can assign id to the script tag and replace it with the new node. 您可以将id分配给脚本标签,并将其替换为新节点。

<p>Foo</p>
<script type="text/javascript" id="placeholder">
    var newElement = document.createElement('div');
    newElement.id='bar';
    var oldElement = document.getElementById('placeholder');
    oldElement.parentNode.replaceChild(newElement, oldElement);
</script>
<p>Baz</p>

And if you need to insert html from string, than you can do it like so: 而且,如果您需要从字符串中插入html,那么您可以这样做:

var div = document.createElement('div');
div.innerHTML = '<div id="bar"></div>';
var placeholder = document.getElementById('placeholder'), 
    container = placeholder.parentNode,
    elems = div.childNodes, 
    el;
while (el = elems[0]) {
    div.removeChild(el);
    container.insertBefore(el, placeholder);
}
container.removeChild(placeholder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM