简体   繁体   English

将 javascript 事件附加到使用 AJAX 加载的内容

[英]Attach javascript event to content loaded with AJAX

previously I had page that was loaded in the usual way with a list of addresses taken from the database and displayed on the page with a php script.以前我的页面以通常的方式加载,其中包含从数据库中获取的地址列表,并使用 php 脚本显示在页面上。 I had on the page a JavaScript code that obfuscated the telephone numbers with the following code.我在页面上有一个 JavaScript 代码,它用以下代码混淆了电话号码。

 <div class="telephoneNumber" data-last="'.$data_last.'" data-first="'.$first_numbers.'"> <span style="cursor:pointer;">'.$data_last.'</span> </div>

 $(document).ready(function(){ $('.telephoneNumber').toggle(function() { $(this).find('span').text(Joomla.JText._('ADVKONTENT_SHOW_TELEPHONE_NUMBER')); },function() { $(this).find('span').text( $(this).data('first') + $(this).data('last') ); }).click(); }

I have change the architecture of the page and we now load the list of addresses using a ajax call and the results injected into the page.我已经更改了页面的架构,现在我们使用 ajax 调用加载地址列表,并将结果注入页面。 The numbers are now not obfuscated.这些数字现在没有被混淆。

I have searched for possible solutions but they all seem to involve some user action such as click.我已经搜索了可能的解决方案,但它们似乎都涉及一些用户操作,例如点击。 Is it possible to rewrite the JavaScript, it does have to be in jquery, so that as the result are loaded into the page we can apply the obfuscate script?是否可以重写 JavaScript,它必须在 jquery 中,以便将结果加载到我们可以应用混淆脚本的页面中?

The solution turned out to be remarkably simple.结果证明解决方案非常简单。 Putting the the code:把代码:

$('.telephoneNumber').toggle(function() {
      $(this).find('span').text(Joomla.JText._('ADVKONTENT_SHOW_TELEPHONE_NUMBER'));
},function() {
  $(this).find('span').text( $(this).data('first') + $(this).data('last') );
}).click();

at the end of the HTML that is returned in the AJAX call means the toggle function is add at the right points and the phone numbers are hidden.在 AJAX 调用中返回的 HTML 的末尾意味着切换 function 被添加到正确的点并且电话号码被隐藏。

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

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