I am new in AJAX. I have searched a lot on Internet but only got basic AJAX steps. Now I am writing codes using AJAX but a common problem I am facing continuously.
我是AJAX的新手。我在互联网上搜索了很多,但只获得了基本的AJAX步骤。现在我正在使用AJAX编写代码,但这是我不断遇到的常见问题。
When I place return text in the particular id of HTML page, Javascript effects do not work. CSS works fine but Javascript effects like table sorting, jQuery effects or any other effect does not work. I know I am missing some concept here. But didn't get any effective answer.
当我将返回文本放在HTML页面的特定ID中时,Javascript效果不起作用。 CSS工作正常但Javascript效果,如表排序,jQuery效果或任何其他效果不起作用。我知道我在这里错过了一些概念。但没有得到任何有效的答案。
Please suggest me what should I do? And what is the concept behind this...
请建议我该怎么办?这背后的概念是什么......
2 个解决方案
#1
3
The new HTML you're adding to the DOM (page) didn't exist when your jquery ran the first time and bound events to elements on the page. You're probably using $("something").click(...) or .bind("click", ...). Instead of these use the delegate function from jquery. Delegate is generally more flexible and faster than live. For instance you can not stopPropagation in a 'live' binding.
当您的jquery第一次运行并将事件绑定到页面上的元素时,您添加到DOM(页面)的新HTML不存在。你可能正在使用$(“something”)。click(...)或.bind(“click”,...)。而不是这些使用jquery的委托函数。代表通常比现场更灵活,更快捷。例如,你无法在“实时”绑定中停止传播。
Why Delegate is better than Live
为什么代表比Live更好
Here is another SO answer that explains the benefits of delegate
这是另一个SO答案,解释了代表的好处
#2
0
What's most likely happening is that your events are getting unbound because you update the DOM with new elements. The easiest solution is to use the live method to bind to events : http://api.jquery.com/live/
最有可能发生的事情是您的事件未被绑定,因为您使用新元素更新DOM。最简单的解决方案是使用live方法绑定到事件:http://api.jquery.com/live/
Or you can simply rebind the events to the elements after insertion to the DOM just as easily.
或者,您可以在插入DOM之后简单地将事件重新绑定到元素。
EDIT
As user kasdega points out, another alternative is to use delegate : http://api.jquery.com/delegate/ Delegate works by using the bound root elements as the context to rebind events to DOM elements that may appear in the future.
正如用户kasdega指出的那样,另一种选择是使用委托:http://api.jquery.com/delegate/ Delegate通过使用绑定的根元素作为上下文来将事件重新绑定到将来可能出现的DOM元素。
#1
3
The new HTML you're adding to the DOM (page) didn't exist when your jquery ran the first time and bound events to elements on the page. You're probably using $("something").click(...) or .bind("click", ...). Instead of these use the delegate function from jquery. Delegate is generally more flexible and faster than live. For instance you can not stopPropagation in a 'live' binding.
当您的jquery第一次运行并将事件绑定到页面上的元素时,您添加到DOM(页面)的新HTML不存在。你可能正在使用$(“something”)。click(...)或.bind(“click”,...)。而不是这些使用jquery的委托函数。代表通常比现场更灵活,更快捷。例如,你无法在“实时”绑定中停止传播。
Why Delegate is better than Live
为什么代表比Live更好
Here is another SO answer that explains the benefits of delegate
这是另一个SO答案,解释了代表的好处
#2
0
What's most likely happening is that your events are getting unbound because you update the DOM with new elements. The easiest solution is to use the live method to bind to events : http://api.jquery.com/live/
最有可能发生的事情是您的事件未被绑定,因为您使用新元素更新DOM。最简单的解决方案是使用live方法绑定到事件:http://api.jquery.com/live/
Or you can simply rebind the events to the elements after insertion to the DOM just as easily.
或者,您可以在插入DOM之后简单地将事件重新绑定到元素。
EDIT
As user kasdega points out, another alternative is to use delegate : http://api.jquery.com/delegate/ Delegate works by using the bound root elements as the context to rebind events to DOM elements that may appear in the future.
正如用户kasdega指出的那样,另一种选择是使用委托:http://api.jquery.com/delegate/ Delegate通过使用绑定的根元素作为上下文来将事件重新绑定到将来可能出现的DOM元素。