简体   繁体   English

ul li独家功能

[英]ul li exclusive function

Imagine the situation like i have a set of crayons, and I want to be able to pick one up, and start drawing, but if -in the middle of the drawing- I change my mind, I still want to be able to change the crayon and pick up a different color. 想象一下这样的情况,例如我有一组蜡笔,我希望能够拿起蜡笔并开始绘制,但是如果-在绘制过程中-我改变了主意,我仍然希望能够更改蜡笔,并选择不同的颜色。

So I have made a ul list of elements, and I want to bind on each of them an object, which has, among its methods, the ability to draw a shape. 所以,我已经取得了ul元素的列表,我想在他们每个人的对象,它,它的方法中,绘制形状的能力结合。

To reach my ideal scenario, each object is exclusive, in the sense that if I click on another li while the previous is active (and the shape is not yet completed), I want the disable the previous li element and enable the current one. 为了达到理想的效果,每个对象都是互斥的,这意味着如果我在前一个li处于活动状态(并且形状尚未完成)时单击另一个li ,则希望禁用前一个li元素并启用当前的li元素。 But also, if I click on the same li while it's still active, it should simply disable the "crayon". 而且,如果在同一个li仍处于活动状态时单击它,则应仅禁用“蜡笔”。

The objects I'm using to draw such shapes, possesses such enable() and disable() function, so I guess it's just a matter of establishing the right combination of controls...which sadly seems I'm not able to do. 我用来绘制此类形状的对象具有此类enable()disable()函数,因此我想这只是建立正确的控件组合的问题……可悲的是,我似乎做不到。

Here's an example: 这是一个例子:

 <ul class='crayon-toolbox'>
     <li id='1' class="crayon"></li>
     <li id='2' class="crayon"></li>
     <li id='3' class="crayon"></li>
 </ul>

 $('.crayon-toolbox').on('click', function(){
    new Crayon();
 })

Can anyone point me out in the right direction? 谁能指出我正确的方向?
It seems to me that every time I click on a li I should check if that li has been clicked, and if not so start the Crayon object...or something like that, but I'm not sure how to check when I click on another li (mind you I want to use only one crayon at all times). 在我看来,每次单击li我都应该检查是否已单击li ,如果不是,则启动Crayon对象...或类似的东西,但是我不确定如何单击时检查li另一只li (注意,我一直想只用一支蜡笔 )。

Thanks in advance! 提前致谢!

It might be a good idea to work with an active class. 与活动班级合作可能是一个好主意。 The properties of each crayon is something you can add in the data perhaps. 您可以在数据中添加每个蜡笔的属性。 Not sure if calling new Crayon() each time is performant ... Instead I suggest to add a method inside the Crayon object that allows you to change it's properties. 不知道每次调用new Crayon()是否都是有效的。相反,我建议在Crayon对象内添加一个方法,以允许您更改其属性。

HTML 的HTML

<ul class='crayon-toolbox'>
    <li class="crayon" data-color="red"></li>
    <li class="crayon" data-color="green"></li>
    <li class="crayon" data-color="blue"></li>
</ul>

JS JS

var anyCrayon = new Crayon();

$('.crayon-toolbox').on('click', function(){
    var crayon = $(this),
        color = crayon.data('color');

    anyCrayon.setColor(color);
});

Not sure to fully understand your question, but from what I've read, you want to create a new object when you click on a crayon, but if the crayon has already been clicked, it doesn't have to create the object. 不确定是否完全理解您的问题,但是根据我的阅读,您想在单击蜡笔时创建一个新对象,但是如果已经单击了蜡笔,则不必创建该对象。

That sound like a job for $.data() ! 听起来像$.data()

Basically, you simply check if the HTML node as a object in its data. 基本上,您只需检查HTML节点是否作为其数据中的对象。 In case it doesn't, you simply assign one. 如果没有,您只需分配一个。

$('.crayon-toolbox').on('click', 'li', function(){ //I've used delegation here, need to target the LI itself for the data.
    var $this = $(this);
    var my_crayon = $this.data('crayon') || new Crayon();

    $this.data('crayon', my_crayon);
});

you can just set different css for selected crayon like below: 您可以为选定的蜡笔设置不同的CSS,如下所示:

$('.crayon').click(function(){
    if($(this).hasClass('selected'))
          {$(this).removeClass('selected'); 
           //if there is a code for disabling current crayon add it here
          }
   else{
          $('.crayon').removeClass('selected');
          $(this).addClass('selected');
          //if there is a code for selecting or disabling crayon add here
   }
});

您可以尝试在每个li上添加onClick事件

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

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