首页 \ 问答 \ 点击时Glyphicon不会旋转(Glyphicon not spinning when clicked)

点击时Glyphicon不会旋转(Glyphicon not spinning when clicked)

我正在编写一个脚本,单击时会旋转图标class =“glyphicon glyphicon-star”。

这是css代码

 .glyphicon-star-animate {
 -webkit-animation-name: rotateThis;
 -webkit-animation-duration: 2s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: linear;
   }

  @-webkit-keyframes "rotateThis" {
  from { 
    -webkit-transform: rotate( 0deg );  
   }
 to  { 
    -webkit-transform: rotate( 360deg ); 
   }
  }

这是jquery代码:

$( document ).ready( function() {
$( "#update" ).on( "click", function( e ) {
var $icon = $( this ).find( ".glyphicon.glyphicon-star" ),
  animateClass = "glyphicon-star-animate";

$icon.addClass( animateClass );
// setTimeout is to indicate some async operation
window.setTimeout( function() {
  $icon.removeClass( animateClass );
}, 2000 );
 });    

});

这是HTML代码:

  <a id="update" href="#"><i class="glyphicon glyphicon-star"></i></a>

我的项目包括显示几个div(使用php的fetcharray循环函数从db中提取),其中包含不同的内容。 但我使用的glyphicon glyphicon-star在我能看到的每个div中是相同的。 我的问题是旋转功能或jquery仅在第一个div上运行。 第二个div或后续类=“glyphicon glyphicon-star”在点击时不会独立旋转。 如何让它们独立旋转?


I am working on a script which spins the icon class="glyphicon glyphicon-star" when clicked.

Here is the css code

 .glyphicon-star-animate {
 -webkit-animation-name: rotateThis;
 -webkit-animation-duration: 2s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: linear;
   }

  @-webkit-keyframes "rotateThis" {
  from { 
    -webkit-transform: rotate( 0deg );  
   }
 to  { 
    -webkit-transform: rotate( 360deg ); 
   }
  }

Here is the jquery code :

$( document ).ready( function() {
$( "#update" ).on( "click", function( e ) {
var $icon = $( this ).find( ".glyphicon.glyphicon-star" ),
  animateClass = "glyphicon-star-animate";

$icon.addClass( animateClass );
// setTimeout is to indicate some async operation
window.setTimeout( function() {
  $icon.removeClass( animateClass );
}, 2000 );
 });    

});

Here is the HTML Code :

  <a id="update" href="#"><i class="glyphicon glyphicon-star"></i></a>

My project consists of displaying several divs(extraction from db using php's fetcharray loop function)with different contents inside it. But the glyphicon glyphicon-star i use is same in every div i could see. My problem is the rotating function or the jquery run only on the first div. The second div or the succeeding class="glyphicon glyphicon-star" is not rotating on click independently. How can i make them rotate independently ?


原文:https://stackoverflow.com/questions/40762583
更新时间:2022-06-11 10:06

最满意答案

我从未成为依赖私有方法(由开发人员用_标记)来实现功能的忠实粉丝,因为这可能会在未来的更新中破坏您的代码。 我检查了你提到的帖子中的所有答案,所有这些方法都有或多或少的问题所以我会尝试给你一个更标准的解决方案。

问题开始于使用data('ui-autocomplete')获取小部件实例,而自动完成插件公开一个调用$(selector).autocomplete('instance')公共方法来实现同样的事情所以不要使用那。

接下来是您需要等待菜单打开并且呈现项目以选择您想要的项目的问题,这必须在事件中完成(请不要使用超时!)。 为此,您有autocompleteopen事件。

这是我第一个解决您问题的方法

$(document).ready(function () {
    ...

    tag.on("autocompleteopen", function (event, ui) {
        // If there are suggestion results
        if (items.lenght > 0) {
            // Select the desired item here
        }
    });

    $('div').click(function () {
        var newVal = $(this).text();
        var oldVal = $('#tags').val();
        tag.val(oldVal + newVal);
        tag.autocomplete('search');         
    })
});

在这种情况下,使用tag.autocomplete('instance')._trigger将无法正常工作。 虽然使用正确的数据触发事件,但菜单不会关闭,并且不会自动选择项目。 您应该在正确的项目上使用click事件。

还有一个问题需要解决,当您的用户在输入内部输入时,您可能希望禁用此功能,这可以通过变量轻松解决。

检查解决方案。

$(document).ready(function() {
  var autoPick = false;
  var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"];
  var tag = $('#tags');
  tag.autocomplete({
    source: aTags
  });
  tag.on("autocompleteopen", function(event, ui) {
    var items = tag.autocomplete('instance').menu.element.children();
    if (items.length > 0 && autoPick) {
      items.eq(0).trigger('click')
      autoPick = false;
    }
  });
  $('div').click(function() {
    var newVal = $(this).text();
    var oldVal = $('#tags').val();
    tag.val(oldVal + newVal);
    autoPick = true;
    tag.autocomplete('search');
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div>e</div>
<div>f</div>
<div>g</div>
<div>h</div>
<div>i</div>
<div>j</div>
<div>k</div>
<div>l</div>
<div>m</div>
<div>n</div>
<div>o</div>
<div>p</div>
<div>q</div>
<div>r</div>
<div>s</div>
<div>t</div>
<div>u</div>
<div>v</div>
<div>w</div>
<div>x</div>
<div>y</div>
<div>z</div>
<input type='text' title='Tags' id='tags' />


I've never been a great fan of relying in private methods (marked with _ by the developer) to achieve functionality because this can break your code in future updates. I checked all the answers in the post you mention and all those methods have more or less issues so I will try to give you a more standard solution.

The problem begins with the use of data('ui-autocomplete') to get the widget instance while the autocomplete plugin expose a public method calling $(selector).autocomplete('instance') to achieve the same thing so don't use that.

Next is the issue that you'll need to wait for the menu to open and the items are rendered to pick the one you want and this must be done in a event (dont use timeouts please!!!). For this you have the autocompleteopen event.

This is my first solution of your problem

$(document).ready(function () {
    ...

    tag.on("autocompleteopen", function (event, ui) {
        // If there are suggestion results
        if (items.lenght > 0) {
            // Select the desired item here
        }
    });

    $('div').click(function () {
        var newVal = $(this).text();
        var oldVal = $('#tags').val();
        tag.val(oldVal + newVal);
        tag.autocomplete('search');         
    })
});

Using tag.autocomplete('instance')._trigger will not work correctly in this case. Although the event is triggered with the correct data the menu won't close and the item wont be automatically selected. You should use a click event on the correct item.

There is another problem you need to solve and is when your users are typing inside the input you probably want this functionality disabled, this can be easily solved with a variable.

Check the solution.

$(document).ready(function() {
  var autoPick = false;
  var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"];
  var tag = $('#tags');
  tag.autocomplete({
    source: aTags
  });
  tag.on("autocompleteopen", function(event, ui) {
    var items = tag.autocomplete('instance').menu.element.children();
    if (items.length > 0 && autoPick) {
      items.eq(0).trigger('click')
      autoPick = false;
    }
  });
  $('div').click(function() {
    var newVal = $(this).text();
    var oldVal = $('#tags').val();
    tag.val(oldVal + newVal);
    autoPick = true;
    tag.autocomplete('search');
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div>e</div>
<div>f</div>
<div>g</div>
<div>h</div>
<div>i</div>
<div>j</div>
<div>k</div>
<div>l</div>
<div>m</div>
<div>n</div>
<div>o</div>
<div>p</div>
<div>q</div>
<div>r</div>
<div>s</div>
<div>t</div>
<div>u</div>
<div>v</div>
<div>w</div>
<div>x</div>
<div>y</div>
<div>z</div>
<input type='text' title='Tags' id='tags' />

相关问答

更多
  • 但jQueryUI文档表示它需要一个扁平的字符串数组。 其实 : 本地数据可以是一个简单的字符串数组 ,也可以包含数组中每个项目的对象 ,并带有标签或值属性或两者。 所以只要你的数据对于数组中的每个对象至少有一个value属性,这个小部件就可以处理它: [ { "name": "John's wild bacon emporium", "code": "BACON", "value": "Bacon", }, { "na ...
  • 只需从数据库中获取现有标记,即可添加selected属性。 我们假设视频标有a和b ,因此当您显示select列表时,它将如下所示:

    相关文章

    更多
  • 友盟分享到朋友圈时, 点击链接跳转到友盟自家页面的处理
  • 7 个免费的 jQuery 图片 360 度旋转插件
  • 关于android自动适应机型分辨率和旋转绘制图片帧的问题.
  • jQuery点击滑过左则菜单
  • 如何根据点击节点不同执行不同action!
  • 请问大家jfreechart必须要生成map文件才能实现热点击吗?
  • Eclipse 插件Hadoop点击没有反应解决方法
  • 网站页面点击率一般是怎样记录的
  • 在java jsp中怎么处理用户多次点击提交按钮造成数据重复。
  • 关于netbeans使用GWT框架失效或点击不显示的问题,最后导致生成失败

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)