duanpin2034 2012-05-10 19:26
浏览 44

如何将JQuery应用于PHP生成的HTML?

I have a PHP file that is generating HTML elements, and is being called from AJAX when the page loads. What I am looking to do is apply JQuery to the dynamically generated elements, so for each time a label, generated from the PHP file, is clicked something happens. I tried placing the JQuery code in the PHP file and echo it out, but that didnt work, I also placed the code in the HTML page, where the AJAX is being called, but that didnt work either.

In my HTML I have:

<body onLoad="javascript:getLabels();">, 

This calls the php file through AJAX:

function getComments() { 
    var thePage ='loadLabels.php'; 
    ... 

The PHP file as the line:

echo '<label id="labellink" class="ldClass">Label </label>'; 

which could be more then one label in the page once loaded, but all in their one <form>, so I need to use JQuery so once the labels are loaded and I click on them, I can make something happen.

  • 写回答

6条回答 默认 最新

  • dongyashun2559 2012-05-10 19:29
    关注

    Edit Thank you for correcting me on that. Since it is depreciated, I haven't used it in a while.

    I'm sure this counts as more of a "hack" than a solution, and I will openly welcome downvotes if necessary, but you could put the post-AJAX functionality within the success function.

    Also, older versions of jQuery used a function called .live() which would look for an element whether it was in the DOM or not. It's depreciated in the newest version of jQuery, but if you're willing to take that leap, I know for a fact that it worked in 1.5. (I'm talking about something like $(a).live('click', function () { });).

    For the record, I would love to have posted this as a comment rather than an answer, but I don't have enough street cred for that yet.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭