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条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容