Javascript 不会更新看似最近的项目的 DOM

发布于 2024-12-19 04:39:58 字数 1984 浏览 0 评论 0原文

我使用 100% 纯 JavaScript,尝试过 Jquery 但没有帮助。代码在 FF/Chrome/Safari 中不起作用。

我构建了就地编辑功能,当用户单击“编辑”(使用 onclick 调用外部函数 - 传入 item_id)时,将隐藏一串文本以显示其中包含相同文本字符串的输入。 (通过更改类)“编辑”也被“保存”取代。编辑完字符串后,用户单击“保存”,一切都会恢复正常。

AJAX 正在处理所有更新 - 但注释掉 AJAX 块并不能修复它。

我正在加载这些对象的流。 javascript适用于所有这些 - 但仅更新 DOM,无论如何在视觉上对于出现的是过去 24 小时之前的项目。这些块本身是相同的。也就是说,当我单击“编辑”时,在过去 18-26 小时内添加的项目不执行任何操作。但是,如果我提醒我要编辑的元素的类,它会显示“正在编辑”(而不是“已保存”),就像它正在工作一样。 (见下文)尽管此更改从未反映在检查元素中。

页面上的代码

<input type="text" class="input_field" id="input_254" value="Foo" onkeydown="javascript: if (event.keyCode == 13) { update(254); }" style="display: none; ">
<span class="user_links" id="display_269" style="display:none;">Foo</span> //hidden span that holds the value and acts at the check
<span id="edit_state_269" class="saved" style="display: none;">Foo</span>
<span onclick="update(269)" id="edit_269">Edit</span>

外部 Javascript

function update(item_id) {

var links_span = document.getElementById('display_' + item_id);
var input_span = document.getElementById('input_' + item_id);
var string_old = document.getElementById('edit_state_' + item_id).innerHTML;
var state_check = document.getElementById('edit_state_' + item_id);
var edit_button = document.getElementById('edit_' + item_id);

if (state_check.getAttribute('class') == 'saved') {

    // Hide the links display list and show the input field
    links_span.style.display = 'none';
    input_span.style.display = 'inline';

    // Change the Edit button text and state_check
    edit_button.innerHTML = 'Save';
    state_check.setAttribute('class','editing');

    //alert(state_check.getAttribute('class')); // this alerts "editing" although in DOM it is still "saved" on the blocks that are the problem

如果有更多详细信息有帮助 - 我会提供它们。

这是一个棘手的问题——没有明显的解决方案。非常感谢您提供的任何指导!

I'm using 100% pure javascript, tried Jquery but it didn't help. Code not working in FF/Chrome/Safari.

I have built Edit-In-Place functionality where when the user clicks "Edit" (calling external function with onclick - passing in item_id) -- a string of text is hidden to reveal an input with the same string of text in it. (by changing classes) "Edit" is also replaced by "Save". When done editing the string - the user clicks save, and everything reverts back to normal.

AJAX is processing all the updates - but commenting out the AJAX block does not fix it.

I am loading a stream of these objects. The javascript works for all of them - but only updates the DOM, visually anyway for what appears is items before the last 24 hours. The blocks themselves are identical. That is - items that have been added within the last 18-26 hours when I click "Edit", do nothing. BUT if I alert out the class of the element I want to edit it says "editing" (as opposed to "saved") like it is working. (see below) Although this change is never reflected in inspect element.

Code on Page

<input type="text" class="input_field" id="input_254" value="Foo" onkeydown="javascript: if (event.keyCode == 13) { update(254); }" style="display: none; ">
<span class="user_links" id="display_269" style="display:none;">Foo</span> //hidden span that holds the value and acts at the check
<span id="edit_state_269" class="saved" style="display: none;">Foo</span>
<span onclick="update(269)" id="edit_269">Edit</span>

External Javascript

function update(item_id) {

var links_span = document.getElementById('display_' + item_id);
var input_span = document.getElementById('input_' + item_id);
var string_old = document.getElementById('edit_state_' + item_id).innerHTML;
var state_check = document.getElementById('edit_state_' + item_id);
var edit_button = document.getElementById('edit_' + item_id);

if (state_check.getAttribute('class') == 'saved') {

    // Hide the links display list and show the input field
    links_span.style.display = 'none';
    input_span.style.display = 'inline';

    // Change the Edit button text and state_check
    edit_button.innerHTML = 'Save';
    state_check.setAttribute('class','editing');

    //alert(state_check.getAttribute('class')); // this alerts "editing" although in DOM it is still "saved" on the blocks that are the problem

If any more details would be helpful - I will provide them.

It is a devil of a problem - with no obvious solution. Would really appreciate any direction you can give!

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

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

发布评论

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

评论(1

瑶笙 2024-12-26 04:39:58

解决了。和往常一样,都是小事。前几个块在页面加载时加载 - 然后在用户导航时隐藏,导致重复的 ID。 JavaScript 自然地选择了页面上较高的那个——隐藏的那个。

Solved. As usual it's the little things. The first few blocks were being loaded on page load - and then hidden as the user navigated resulting in duplicate IDs. Javascript naturally selected the one higher on the page - the one that was hidden.

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