简体   繁体   English

来自Jquery的HTML响应不适用于javascript

[英]Html response from Jquery not working with javascript

I have find a lot of posts similar to this question but I don't think I'm doing it wrong since I'm not putting javascript in the response of the ajax call. 我发现很多与此问题类似的帖子,但我认为我做错了,因为我没有将javascript放在ajax调用的响应中。

Basically the ajax returns pure html that is put inside a div. 基本上,ajax返回放置在div内的纯html。 This div is inside my page and the page already contains references to css and javascript files. 该div位于我的页面内部,并且该页面已经包含对CSS和javascript文件的引用。

In one of those javascript files I try to render the buttons returned by the ajax call to look nice with a jquery ui function so basically what I do is this 在其中的一个javascript文件中,我尝试呈现由ajax调用返回的按钮,以使用jQuery ui函数看起来不错,所以基本上我要做的就是

$(".ButtonClass").button();

This piece of code is in the document.ready function, and the html returned by ajax includes the "ButtonClass" for all the buttons. 这段代码在document.ready函数中,ajax返回的html包含所有按钮的“ ButtonClass”。

The .button() works for a button outside the returned ajax html, but it does not work for elements returned by the ajax call. .button()适用于返回的ajax html外部的按钮,但不适用于ajax调用返回的元素。

I think it should work because css styles are applied to the results but I cannot get any function to work with the results, i repeat I don't return javascript code I have this code in an external file which is referenced in the whole page. 我认为它应该工作,因为将CSS样式应用于结果,但是我无法获得任何功能来处理结果,我重复一遍,我不返回javascript代码,我将此代码保存在整个页面中引用的外部文件中。

If you want to enable buttons for elements returned by the AJAX request, you should execute it again when receiving the response. 如果要为AJAX请求返回的元素启用按钮,则应在收到响应时再次执行它。

.button() is executed on the elements that have been selected at the time it was called - which does not include the AJAX ones when called before the AJAX request was made. .button()在调用时已选择的元素上执行-在发出AJAX请求之前调用时不包括AJAX元素。

Please do note that $(".ButtonClass") will also include previous .ButtonClass elements, so they will be converted to buttons twice. 请注意, $(".ButtonClass")也将包含以前的.ButtonClass元素,因此它们将两次转换为按钮。 You'd have to select only the ones from the AJAX response. 您只需要从AJAX响应中选择一个即可。

Try calling 尝试致电

$(".ButtonClass").button();

In ajax callback. 在ajax回调中。

I think you need to apply your decorator function when the ajax call has been succesfully terminated. 我认为当ajax调用成功终止时,您需要应用装饰器函数。 Something along this lines: 遵循以下原则:

$('#myelement').load('script.php', function() {

   // Now the page has some more button to decorate:
   $(".ButtonClass .ugly").button().removeClass('ugly'); 

});

As you may see I added a "ugly" class to the new buttons, so to not re-beautify the already beatiful buttons present :) 如您所见,我在新按钮上添加了“丑陋”类,以免重新美化存在的美丽按钮:)

The .button() function must be called every time new markup is introduced in the page that should be a button. 每次在应为按钮的页面中引入新标记时,都必须调用.button()函数。 You do this correctly on .ready(), but you should also do it on the success function of the ajax call. 您可以在.ready()上正确执行此操作,但也应该在ajax调用的成功函数上执行此操作。

You will have to initialize these buttons after you populate your div. 填充div后,您将必须初始化这些按钮。

If your div is stored in, for instance, a javascript variable called $container , then run the code $('.ButtonClass',$container).button() after you have added this content to your page. 例如,如果您的div存储在名为$container的javascript变量$container ,则在将内容添加到页面后,运行代码$('.ButtonClass',$container).button()

in your callback for the ajax, do this $(".ButtonClass").button(); 在您的Ajax回调中,执行$(".ButtonClass").button(); again. 再次。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM