jQuery 的替换: $('#id').html(HTMLfragment) // 包括。<脚本>执行

发布于 2024-11-15 07:18:29 字数 7460 浏览 2 评论 0原文

HTMLfragment 是包含

document.getElementById('id').innerHTML = HTMLfragment;

不起作用(所有浏览器,包括 Chrome)。它仅适用于其中不包含任何

更新

我的responseText...

<style>
    label, input {
        display: block;
    }
</style>

<script>
    function edit(cmdName) {
        document.getElementById("edit-form-" + cmdName).style.display = "block";
    }

    function hide(cmdName) {
        document.getElementById("edit-form-" + cmdName).style.display = "none";
    }

    var applyValues = function() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/cmd/list.json', false);
        xhr.onload = function(e) {
            fillPager(JSON.parse(this.responseText).cmds);
        };
        xhr.send();
    };


    applyValues();


    function cmdDelete(cmdName) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', '/cmd/delete?name=' + cmdName, true);
        xhr.onload = function(e) {
            location.reload();
        };
        xhr.send(null);
    }

    function sendForm(form) {
        var formData = new FormData(form);
        var xhr = new XMLHttpRequest();
        xhr.open('post', form.action, true);
        xhr.send(formData);
        return false; // Prevent page from submitting.
    }

    function fillPager(cmdsJson) {
        var pager = document.getElementById('pager');
        for (var i in cmdsJson) {
            var row = document.createElement('tr');

            var cellName = document.createElement('td');
            var cellRESTcall = document.createElement('td');
            var cellDesc = document.createElement('td');
            var cellCreator = document.createElement('td');
            var cellUser = document.createElement('td');
            var cellCreated = document.createElement('td');
            var cellUpdated = document.createElement('td');
            var cellDelete = document.createElement('td');

            function buildUpdationForm() {
                var updateForm = document.createElement('form');
                var fieldset = document.createElement('fieldset');
                var inputName = document.createElement('input');
                inputName.setAttribute('value', cmdsJson[i].Name);
                inputName.setAttribute('name', 'edit-name');
                inputName.setAttribute('id', 'edit-name');
                inputName.setAttribute('readonly', true);
                var inputShortcut = document.createElement('input');
                inputShortcut.setAttribute('value', cmdsJson[i].RESTcall);
                inputShortcut.setAttribute('name', 'edit-restCall');
                inputShortcut.setAttribute('id', 'edit-restCall');
                var inputDesc = document.createElement('input');
                inputDesc.setAttribute('value', cmdsJson[i].Desc);
                inputDesc.setAttribute('name', 'edit-desc');
                inputDesc.setAttribute('id', 'edit-desc');
                var updateButton = document.createElement('button');
                var updateButtonValue = document.createTextNode("Update");
                updateButton.appendChild(updateButtonValue);
                updateButton.setAttribute('onclick', 'location.reload(); hide("' + cmdsJson[i].Name + '"); return sendForm(this.form);');

                updateForm.setAttribute('style', 'display: none;');
                updateForm.setAttribute('id', 'edit-form-' + cmdsJson[i].Name);
                updateForm.setAttribute('action', '/cmd/update');
                updateForm.setAttribute('method', 'post');

                fieldset.appendChild(inputName);
                fieldset.appendChild(inputShortcut);
                fieldset.appendChild(inputDesc);
                fieldset.appendChild(updateButton);
                cellName.appendChild(updateForm);

                updateForm.appendChild(fieldset);
            }

            buildUpdationForm();

            var cellNameLink = document.createElement('span');
            cellNameLink.innerHTML = '<a href="javascript:edit(\'' + cmdsJson[i].Name + '\');">' + cmdsJson[i].Name + '</a>';
            var cellRESTcallTxt = document.createTextNode(cmdsJson[i].RESTcall);
            var cellDescTxt = document.createTextNode(cmdsJson[i].Desc);
            var cellCreatorTxt = document.createTextNode(cmdsJson[i].Creator);
            var cellUserTxt = document.createTextNode(cmdsJson[i].User);
            var cellCreatedTxt = document.createTextNode(cmdsJson[i].Created);
            var cellUpdatedTxt = document.createTextNode(cmdsJson[i].Updated);
            var cellDeleteLink = document.createElement('button');
            cellDeleteLink.innerHTML = 'X';
            cellDeleteLink.setAttribute('onclick', 'cmdDelete("' + cmdsJson[i].Name + '");');

            cellName.appendChild(cellNameLink);
            cellRESTcall.appendChild(cellRESTcallTxt);
            cellDesc.appendChild(cellDescTxt);
            cellCreator.appendChild(cellCreatorTxt);
            cellUser.appendChild(cellUserTxt);
            cellCreated.appendChild(cellCreatedTxt);
            cellUpdated.appendChild(cellUpdatedTxt);
            cellDelete.appendChild(cellDeleteLink);

            row.appendChild(cellName);
            row.appendChild(cellRESTcall);
            row.appendChild(cellDesc);
            row.appendChild(cellCreator);
            row.appendChild(cellUser);
            row.appendChild(cellCreated);
            row.appendChild(cellUpdated);
            row.appendChild(cellDelete);

            pager.appendChild(row);
        }
    }
</script>

<form action="/cmd/create" method="post">
    <fieldset>
        <legend>Command</legend>
        <label for="name">Name</label>
        <input name="name" id="name" type="text" value="name"/>
        <label for="restCall">Shortcut</label>
        <input name="restCall" id="restCall" type="text" value="rc"/>
        <label for="desc">Description</label>
        <input name="desc" id="desc" type="text" value="desc"/>
        <input type="submit" value="Create">
    </fieldset>
</form>

<table id="pager">
    <tr>
        <th>Name</th>
        <th>RESTcall</th>
        <th>Description</th>
        <th>Creator</th>
        <th>User</th>
        <th>Created</th>
        <th>Updated</th>
        <th>Delete</th>
    </tr>
</table>

...是我在执行此代码片段时得到的XHR2 responseText:

            var xhr = new XMLHttpRequest();
            xhr.open('GET', handlerMap[window.location.pathname], false);
            xhr.onload = function(e) {
                console.log(this.responseText);
//                $('#main').html(this.responseText);
                document.getElementById('main').innerHTML = this.responseText;
            };
            xhr.send();

注释的jQuery方法用于将responseText内容分配给ID为“main”的DIV元素,但常规 JavaScript 方法不会。 使用文档方法,仅应用非JavaScript部分(我可以看到表格和表单),但无法执行JavaScript部分。

那么如何在不使用 jQuery 的情况下将 responseText 分配给 DIV 元素呢?

HTMLfragment is a HTML fragment that contains <script> parts. Therefore

document.getElementById('id').innerHTML = HTMLfragment;

doesn't work (all browsers incl. Chrome). It works only for HTML fragments that don't have any <script> parts in it. So how can I assign a HTML fragment that has <script> parts in it to a DIV using Google Closure or — even better — plain JavaScript?

Update

My responseText...

<style>
    label, input {
        display: block;
    }
</style>

<script>
    function edit(cmdName) {
        document.getElementById("edit-form-" + cmdName).style.display = "block";
    }

    function hide(cmdName) {
        document.getElementById("edit-form-" + cmdName).style.display = "none";
    }

    var applyValues = function() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/cmd/list.json', false);
        xhr.onload = function(e) {
            fillPager(JSON.parse(this.responseText).cmds);
        };
        xhr.send();
    };


    applyValues();


    function cmdDelete(cmdName) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', '/cmd/delete?name=' + cmdName, true);
        xhr.onload = function(e) {
            location.reload();
        };
        xhr.send(null);
    }

    function sendForm(form) {
        var formData = new FormData(form);
        var xhr = new XMLHttpRequest();
        xhr.open('post', form.action, true);
        xhr.send(formData);
        return false; // Prevent page from submitting.
    }

    function fillPager(cmdsJson) {
        var pager = document.getElementById('pager');
        for (var i in cmdsJson) {
            var row = document.createElement('tr');

            var cellName = document.createElement('td');
            var cellRESTcall = document.createElement('td');
            var cellDesc = document.createElement('td');
            var cellCreator = document.createElement('td');
            var cellUser = document.createElement('td');
            var cellCreated = document.createElement('td');
            var cellUpdated = document.createElement('td');
            var cellDelete = document.createElement('td');

            function buildUpdationForm() {
                var updateForm = document.createElement('form');
                var fieldset = document.createElement('fieldset');
                var inputName = document.createElement('input');
                inputName.setAttribute('value', cmdsJson[i].Name);
                inputName.setAttribute('name', 'edit-name');
                inputName.setAttribute('id', 'edit-name');
                inputName.setAttribute('readonly', true);
                var inputShortcut = document.createElement('input');
                inputShortcut.setAttribute('value', cmdsJson[i].RESTcall);
                inputShortcut.setAttribute('name', 'edit-restCall');
                inputShortcut.setAttribute('id', 'edit-restCall');
                var inputDesc = document.createElement('input');
                inputDesc.setAttribute('value', cmdsJson[i].Desc);
                inputDesc.setAttribute('name', 'edit-desc');
                inputDesc.setAttribute('id', 'edit-desc');
                var updateButton = document.createElement('button');
                var updateButtonValue = document.createTextNode("Update");
                updateButton.appendChild(updateButtonValue);
                updateButton.setAttribute('onclick', 'location.reload(); hide("' + cmdsJson[i].Name + '"); return sendForm(this.form);');

                updateForm.setAttribute('style', 'display: none;');
                updateForm.setAttribute('id', 'edit-form-' + cmdsJson[i].Name);
                updateForm.setAttribute('action', '/cmd/update');
                updateForm.setAttribute('method', 'post');

                fieldset.appendChild(inputName);
                fieldset.appendChild(inputShortcut);
                fieldset.appendChild(inputDesc);
                fieldset.appendChild(updateButton);
                cellName.appendChild(updateForm);

                updateForm.appendChild(fieldset);
            }

            buildUpdationForm();

            var cellNameLink = document.createElement('span');
            cellNameLink.innerHTML = '<a href="javascript:edit(\'' + cmdsJson[i].Name + '\');">' + cmdsJson[i].Name + '</a>';
            var cellRESTcallTxt = document.createTextNode(cmdsJson[i].RESTcall);
            var cellDescTxt = document.createTextNode(cmdsJson[i].Desc);
            var cellCreatorTxt = document.createTextNode(cmdsJson[i].Creator);
            var cellUserTxt = document.createTextNode(cmdsJson[i].User);
            var cellCreatedTxt = document.createTextNode(cmdsJson[i].Created);
            var cellUpdatedTxt = document.createTextNode(cmdsJson[i].Updated);
            var cellDeleteLink = document.createElement('button');
            cellDeleteLink.innerHTML = 'X';
            cellDeleteLink.setAttribute('onclick', 'cmdDelete("' + cmdsJson[i].Name + '");');

            cellName.appendChild(cellNameLink);
            cellRESTcall.appendChild(cellRESTcallTxt);
            cellDesc.appendChild(cellDescTxt);
            cellCreator.appendChild(cellCreatorTxt);
            cellUser.appendChild(cellUserTxt);
            cellCreated.appendChild(cellCreatedTxt);
            cellUpdated.appendChild(cellUpdatedTxt);
            cellDelete.appendChild(cellDeleteLink);

            row.appendChild(cellName);
            row.appendChild(cellRESTcall);
            row.appendChild(cellDesc);
            row.appendChild(cellCreator);
            row.appendChild(cellUser);
            row.appendChild(cellCreated);
            row.appendChild(cellUpdated);
            row.appendChild(cellDelete);

            pager.appendChild(row);
        }
    }
</script>

<form action="/cmd/create" method="post">
    <fieldset>
        <legend>Command</legend>
        <label for="name">Name</label>
        <input name="name" id="name" type="text" value="name"/>
        <label for="restCall">Shortcut</label>
        <input name="restCall" id="restCall" type="text" value="rc"/>
        <label for="desc">Description</label>
        <input name="desc" id="desc" type="text" value="desc"/>
        <input type="submit" value="Create">
    </fieldset>
</form>

<table id="pager">
    <tr>
        <th>Name</th>
        <th>RESTcall</th>
        <th>Description</th>
        <th>Creator</th>
        <th>User</th>
        <th>Created</th>
        <th>Updated</th>
        <th>Delete</th>
    </tr>
</table>

...is what I get as my XHR2 responseText executing this snippet:

            var xhr = new XMLHttpRequest();
            xhr.open('GET', handlerMap[window.location.pathname], false);
            xhr.onload = function(e) {
                console.log(this.responseText);
//                $('#main').html(this.responseText);
                document.getElementById('main').innerHTML = this.responseText;
            };
            xhr.send();

The commented jQuery method works to assign the responseText content to the DIV element with the ID "main" but the regular JavaScript method doesn't. Using the document method, just the non-JavaScript part is applied (I can see the table & form) but the JavaScript part can't be executed.

So how can I assign responseText to the DIV element without using jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2024-11-22 07:18:29
$('#id').append('<script type="text/javascript" src="http://somesite.com/somejs.js"></script>');

这会将脚本附加到#id innerHTML 的末尾。很容易。风格是一样的。

你也可以这样做:

var s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = "//write your js code here"+
              "//and here ";
document.getElementsByTagName("head")[0].appendChild(s);

这会让你做同样的事情。

$('#id').append('<script type="text/javascript" src="http://somesite.com/somejs.js"></script>');

That would append the script to the end of the #id innerHTML. Pretty easy. Style would be the same thing.

You can also do:

var s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = "//write your js code here"+
              "//and here ";
document.getElementsByTagName("head")[0].appendChild(s);

That would allow you to do the same thing.

遇见了你 2024-11-22 07:18:29
document.getElementById('question').innerHTML = '<style>* { color:red; }</style>'; 

例如,我在 SO 上工作。你能显示更大的不起作用的代码片段吗?
您还发现它在哪些浏览器中出现故障?

document.getElementById('question').innerHTML = '<style>* { color:red; }</style>'; 

works for me on SO for instance. can you show larger codesnippet that doesn't work?
also in which browsers have you found it to malfunction ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文