点击时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
最满意答案
我从未成为依赖私有方法(由开发人员用_标记)来实现功能的忠实粉丝,因为这可能会在未来的更新中破坏您的代码。 我检查了你提到的帖子中的所有答案,所有这些方法都有或多或少的问题所以我会尝试给你一个更标准的解决方案。
问题开始于使用
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列表时,它将如下所示:
-
我认为使用单独的事件源会更清晰。 IObservable
mouseMove = GetMouseMove(); // stream of MouseMove events IObservable manualTrigger = new Subject (); var positionChange = mouseMove.Merge(manualTrigger); positionChange.Subscribe(pos => ...); 现在 ... -
在这里,尝试这样做: var availableTags = [ "Perl", "PHP", "Python", "Ruby" ]; $('input#mainSearchBox').autocomplete({ source: availableTags, minLength: 0 }); $('input#mainSearchBox').autocomplete("disable"); $('input#mainSearchBox').keyup( ...
-
我不确定它是否是最好的,但我找到了一种让它工作的方法(仍在倾听更好的答案)。 当我调用服务并注意到工作会话已过期时,我所做的就是删除Web会话中的活动用户。 我的自定义authorize属性也实现了IResultFilter和IExceptionFilter 。 在OnResultExecuted和OnException我再次验证活动用户。 如果会话被删除,则应用我将在OnAuthorization应用的相同ActionResult 。 这是最后一堂课: public class CustomAuthori ...
-
为什么不将select事件处理程序中的逻辑提取到一个可以从Javascript代码中调用的独立函数? 您在该函数中所需要的只是国家/地区输入(在您当前的代码中,以及所选的国家/地区json,即ui.item ); 因此,您可以将当前逻辑提取到一个新的函数countrySelected : var countrySelected = function (countryInput, countryJSON) { $(countryInput).val(countryJSON.label).attr("o ...
-
如何选择手动选择?(how select option of select manually?)[2023-09-15]
[ngValue] ="-1" [ngValue] = "p['id']" 你很接近,我认为你需要将值改为ngValue 我最近问了一个类似的问题: 设置默认选择列表值Angular2 您需要在.ts文件中将product的值设置为-1,并将ngValue设置为-1 [ngValue] ="-1" [ngValue] = "p['id']" You're close, I think you need to change value to ngValue I recently asked a simil ... -
jQueryUI自动完成 - 将搜索结果放在DOM中的输入旁边(jQueryUI autocomplete - place search results adjacent to input in DOM)[2022-02-05]
这似乎应该在jQuery UI中原生支持! 无论如何,这里有一些我认为会起作用的东西: $("#X").autocomplete({ source: ['abc', 'def', 'ghi'], open: function($event, ui) { var $widget = $("ul.ui-autocomplete"); var $input = $("#X"); var position = $input.position(); ... -
我从未成为依赖私有方法(由开发人员用_标记)来实现功能的忠实粉丝,因为这可能会在未来的更新中破坏您的代码。 我检查了你提到的帖子中的所有答案,所有这些方法都有或多或少的问题所以我会尝试给你一个更标准的解决方案。 问题开始于使用data('ui-autocomplete')获取小部件实例,而自动完成插件公开一个调用$(selector).autocomplete('instance')的公共方法来实现同样的事情所以不要使用那。 接下来是您需要等待菜单打开并且呈现项目以选择您想要的项目的问题,这必须在事件中完成 ...
-
jqueryUI自动完成 - 无论搜索结果如何,始终附加选项(jqueryUI autocomplete - always append an option regardless of search results)[2022-10-08]
这是您缺少的方法,您只需要访问ui.content属性。 response: function( event, ui ) { //console.log(ui.content); ui.content.unshift({value:"Add new account", label:"Add new account"}); } 这是工作的例子。 $( function() { var availableTags = [ "ActionScript", ...