简体   繁体   English

如何让DOM知道动态创建的新元素?

[英]How do I let the DOM know about new elements created on the fly?

I am making an AJAX call and I am building some HTML on the backend side. 我正在拨打AJAX,并在后端构建一些HTML。 I am returning them as a JSON and then using jQuery I render them, something like: 我将它们作为JSON返回,然后使用jQuery渲染它们,类似于:

$(data.message).each(function (index, value) {
    $('#states_table tbody').append('<td>' + value.edit_link + '</td>');
});

value.edit_link contains the following HTML: value.edit_link包含以下HTML:

<a id="edit_0" 
   title="Edit" 
   onclick="javascript:return false;" 
   class="edit"
>Edit</a>

Then I will end up with something like this: 然后,我将得到这样的结果:

在此处输入图片说明

And as you can see the link doesn't have the hand cursor. 如您所见,链接没有手形光标。 My guess is that the DOM doesn't know about them when the page is rendered for first time so no CSS properties or default properties will be applied which makes me ask: 我的猜测是,第一次呈现页面时DOM不了解它们,因此不会应用CSS属性或默认属性,这使我问:

In order to have the links with the cursor: hand , how do I let the DOM know about this new elements? 为了使带有cursor: hand的链接cursor: hand ,如何让DOM知道这个新元素?

Solution 1 解决方案1

Add to your link html href="#" or href="javascript:void(0)" so: 将html href="#"href="javascript:void(0)"添加到您的链接中,以便:

<a href="#"
   id="edit_0" 
   title="Edit" 
   onclick="javascript:return false;" 
   class="edit"
>Edit</a>

For a full comprehension of <a> html tag read this . 要全面了解<a> html标签,请阅读此内容

If you don't know if is better href="#" or href="javascript:void(0)" (nobody knows) read this discussion . 如果您不知道更好的方法href="#"href="javascript:void(0)" (没人知道),请阅读此讨论

Solution 2 解决方案2

Don't use an <a> but a <button> following the sentiment : 不要在情感之后使用<a>而是使用<button>

if <a> button doesn't have a meaningful href, it's a <button> 如果<a>按钮没有有意义的href,则为<button>

(for someone a <button> outside a <form> feels wrong. You decide.) (对于某人, <form>之外的<button> <form>感觉不对。您决定。)

so: 所以:

<button id="edit_0" 
   onclick="javascript:return false;" 
   class="edit"
>Edit</button>

Button reference there . 按钮参考那里

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

相关问题 帮助您理解如何在运行中创建的新dom元素上执行javascript背后的逻辑 - help with understanding the logic behind how javascript executes on new dom elements being created on the fly 我如何知道何时创建DOM元素? - How do I know when a DOM-element is created? 如何让js知道新创建的html元素 - how to let js know about newly created html element 即时更新DOM以适应新元素 - Updating DOM on the fly to fit new elements JSDoc:如何让 IntelliSense/TypeScript 了解其他文件中的类? - JSDoc: How do I let IntelliSense/TypeScript know about classes from other files? 在DOM上动态创建的元素上触发事件,这可能吗? - Trigger event on elements created on the fly at DOM, it's possible? 如何为动态添加到DOM的元素初始化DateTimePicker? - How to initialize DateTimePicker for elements added to the DOM on the fly? 如果不知道何时创建事件侦听器,如何将事件侦听器添加到HTML元素? - How do I add event listeners to HTML elements if I do not know when they will be created? 我如何让jquery识别使用.on在fly元素上创建的? - How do I get jquery to recognize created on the fly element with .on? 如何获取使用 DOM 而不是 HTML 创建的元素? - How do I get elements that were created using the DOM instead of HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM