元素加载到Ajax页面后,如何使用jQuery运行一些Javascript代码? [英] how to run call some Javascript code with jQuery as soon as element loads to page by Ajax?

查看:38
本文介绍了元素加载到Ajax页面后,如何使用jQuery运行一些Javascript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些html标签由Ajax加载到页面时为它们分配一些属性,但是我不想在我的页面中编写任何Javascript.敌人的例子中,我有如下所示的html :(请记住,这是通过ajax来实现的,而不是在页面加载时实现的)

I'm trying to assign some attributes to some html tags as soon as they load to the page by Ajax, but I don't want to write any Javascript in my page. foe example I have the the html like below:(remember that this comes via ajax, not on page load)

<div data-something="true">
 something here
</div>

我想将这样的内容保存到.js文件中:(这不起作用)

and I want to have something like this which is saved to a .js file: (this doesn't work)

$('body').on('load', '[data-something]', function(){  
       //do something 
});

但是我不想在一页中有这样的内容:

but I do NOT want to have like this in one page :

<div data-something="true">
 something here
</div>

<script>
   $(function(){
        //do something
   });
</script>

反正有这样做吗?在元素加载后立即触发事件?

is there anyway to do so? to trigger the event as soon as the element being loaded?

推荐答案

当然,将其放在ajax调用成功的情况下.

Sure, put it in the success of the ajax call.

$("someelemnt").load("somepage.html",function(){
    // do something.
});

不要在通过ajax加载的页面上使用脚本标记,它们通常会在将html附加到页面上之前执行.

do not use script tags on a page that you are loading via ajax, they'll usually get executed before the html is appended to the page.

这篇关于元素加载到Ajax页面后,如何使用jQuery运行一些Javascript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆