douguyi3903 2010-07-16 22:55
浏览 35
已采纳

允许用户在页面上选择任意元素

I'm coding a templating system that is supposed to support easily converting any old html/xhtml form into a format that can be used with my system (it's a template system for my cms).

Here's what I'm aiming for:

  1. The user clicks a "Zone Define" button for example: "Content", "Sidebar", etc
  2. When the user hovers any elements they become get a border or are highlighted somehow
  3. If the user clicks that element it is modified to have a class like 'zone-content'
  4. The page sends the new html to the script via ajax

Is there any already existing libraries that would facilitate something like this?

UPDATE:

I ended up using the answers below and hacking together a jquery solution (we're going to use jquery in the rest of the project). It's a bit ugly and hacky but it works http://ilsken.com/labs/template/

  • 写回答

3条回答 默认 最新

  • donglian6625 2010-07-16 23:16
    关注

    I personally am a fan of ExtJS (now Sencha), and this is fairly simple and straightforward to do using that framework. This is how I'd do it:

    <body>
      <button id="content_define" class="zone_define">Content</button>
      <button id="sidebar_define" class="zone_define">Sidebar</button>
      <div id="template">
        <!-- put your template here -->
      </div>
    </body>
    
    <script type="text/javascript">
    Ext.onReady(function() {
      Ext.select('button.zone_define').on('click', function(ev, target) {
        ev.stopEvent();
    
        // this listener is bound to two buttons, determine which one was pressed
        var zone = target.id.split('_')[0]; // 'content' or 'sidebar'
    
        // add a click listener listener to the template so that when an element within is clicked,
        // an Ajax request is initiated
        var template = Ext.get('template');
        template.addClass('targetable').on('click', function(ev, target) {
          template.removeClass('targetable');
    
          // this part is optional, in case the user clicks an <a> or <img> element,
          // you may want to get the block element containing the clicked element
          var target = ev.getTarget('div'); // or ul, table, p, whatever
    
          // invoke zone_capture on the click target (or containing block element)
          zone_capture(zone, target);
        }, window, {single: true});
        // the {single: true} part removes this listener after one click,
        // so the user has to go back and press the button again if they want to redefine
      });
    
      // this is the code to do the ajax request
      // and also add a class to the target element
      function zone_capture(zone, target) {
        // first, remove the class from elements which are already defined as this zone
        Ext.select(String.format('#template .{0}_zone', zone)).removeClass(zone + '_zone');
    
        // now add that class to the target
        target.addClass(zone + '_zone');
    
        // do the POST
        Ext.Ajax.request({
          method: 'POST',
          url: '/region/define',
          params: { // these are the params POSTed to your script
            template: Ext.get('template').dom.innerHTML
          }
        });
      }
    });
    </script>
    

    I would also add the following CSS rules for hovering effects, etc.

    #template.targetable:hover div { /* add div, p, table, whatever */
      border: 1px solid #f00;
    }
    
    #template.targetable {
      cursor: crosshair;
    }
    

    I would call this pseduo-code, but the idea should be enough to get you started. Since you are dealing with user input, you will have a lot of corner cases to check for before you can call this production-ready. It sounds like a cool way to define the template, good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算