dongyun9120 2013-01-23 17:00
浏览 78
已采纳

jQuery / PHP / Smarty - 非侵入式JS - 设置动态元素ID处理程序(PHP变量)

I'm trying to separate html and jQuery events handlers in my webapp. Here's my context : my webapp works with tabs with ajax loaded content. Each tab content is identified by an unique id. In each tab I got a table. Two or more tabs contents can be the same (like in Chrome for an example, the same website can be loaded in two tabs). In a tab, each table row is editable by clicking a jQuery initialized button.

Here is a simplified example how it currently works :

SMARTY TEMPLATE

<div id="tabContent_{$uniqueId}">
  <table>
    {foreach $operations as $op}
       <tr><td><button id="buttonEditOperation_${op}" /></td></tr>
    {/foreach}
  </table>
</div>

<script>           
     $('#tabContent_{$uniqueId} [id^="buttonEditOperation_"]')
       .click( function() { alert('CLICK !'); })
       .button(blabla);
</script>

When I load a new tab with the same content, only buttons in this new content are initialized, because of #tabContent_{$uniqueId} selector

I would like to create a javascript file to separate JS and my SMARTY TEMPLATE but I don't know how to select only the content of the new loaded tab. I would like something like this :

SMARTY TEMPLATE

<script type="text/javascript" src="./js/buttonInit.js"></script>
<div id="tabContent_{$uniqueId}">
  <table>
     {foreach $operations as $op}
       <tr><td><button id="buttonEditOperation_${op}" /></td></tr>
    {/foreach}
  </table>
</div>

JS FILE

<script> 
     $('#tabContent_?????? [id^="buttonEditOperation_"]')  // HERE IS MY PROBLEM
       .click( function() { alert('CLICK !'); })
       .button(blabla);
</script>

I have some ideas (HTML5 data properties, initialization function, global variable, etc.) but I would like to know the best solution for this problem.

Thanks for your feedback !

  • 写回答

2条回答 默认 最新

  • doufan6033 2013-03-04 12:45
    关注

    See below the best solution I've found ! Do not hesitate to tell me if I'm doing something wrong...

    SMARTY TEMPLATE

    <script type="text/javascript" src="./js/object.Module.js"></script>
    <script type="text/javascript" src="./js/object.Operation.js"></script>
    <div id="tabContent_{$uniqueId}">
      <table>
         {foreach $operations as $op}
           <tr><td>
              <button id="buttonEditOperation_${op}" />
              <span class="tooltip" title="Tooltip content">Show tooltip</span>
           </td></tr>
        {/foreach}
      </table>
    </div>
    
    <script>
        // Create a new object "Operation" which inherit from "Module"
        var currentModule_{$uniqueId} = new Operation( {$uniqueId} );
    
        // Use a method from main object "Module"
        currentModule_{$uniqueId}.init_tooltips();
    
        // Use a method from specific object "Operation"
        currentModule_{$uniqueId}.init_buttonEditOperation('CLICK !');
    </script>
    

    MAIN JS FILE : "object.Module.js"

    var Module = function(_uniqueId) {
        // Store the value of this in a different variable because jQuery
        // override it when handling events
        // (http://stackoverflow.com/questions/10596727/this-keyword-overriden-in-javascript-class-when-handling-jquery-events)
        var self = this;
    
    
        /* ========================================= */
        /* PRIVATE ATTRIBUTE */   
        /* ========================================= */
        // Initialized w/ arguments values when creating "object"
        // (e.g. : var myVar = new Module("35d98q452e");
        self.uniqueId = _uniqueId; // e.g. : "35d98q452e"
    
    
        /* ========================================= */
        /* PUBLIC METHODS */   
        /* ========================================= */    
        /*-------------------------------------
        /* ---> Initialize tooltips */
        self.constructor.prototype.init_tooltips = function() {
             $( "#tabContent_" + self.uniqueId + " .tooltip" ).each(function(index) {
                $(this).tooltip();
            }); 
        }
    
    };
    

    MODULE "OPERATION" SPECIFIC JS FILE "object.Operation.js"

    var Operation = function(_uniqueId) {
        var self = this;
    
        /* ========================================= */
        /* INHERIT FROM PARENT OBJECT "Module" */   
        /* ========================================= */
        Module.call(self, _uniqueId);
    
    
        /* ========================================= */
        /* PUBLIC METHODS */   
        /* ========================================= */ 
        /*-------------------------------------
        /* ---> Initialize button "Edit operation" */
        self.init_buttonEditOperation = function(_msg) {
           $('#tabContent_' + self.uniqueId + ' [id^="buttonEditOperation_"]')
             .click( function() { alert(_msg); })
             .button(blabla);
        }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀