jQuery Highlight element on select option

戏子无情 提交于 2019-12-13 23:19:40

问题


I have a list of values in a drop down style select box e.g.

<select id="places">
<option>Italy</option>
<option>France</option>
<option>Germany</option>
<option>Spain</option>
</select>

I also have the same list of values in a div on my page e.g.

<div>
<span>Italy</span>
<span>France</span>
<span>Germany</span>
<span>Spain</span>
</div>

Using JQuery, I want to have it so - when a value in the dropdown is selected, the equivalent option in the div is briefly highlighted.

I've been struggling with the jQuery highlight plugin, but I believe the quicker way may be to use the highlight class of JjQuery UI. Apologies in advance for being a noob :)


回答1:


Using the highlight effect from jQuery UI:

$('#places').change(function() {
  $('div span:contains(' + $(this).val() + ')').effect('highlight', {}, 1000)
})

When an item from the drop-down is selected, the span containing the respective text of the selected item is run through a animation of its background color ("highlight" effect) provided by the plugin.

Demo here.




回答2:


Using JQuery UI plugin you can use the following

$('#places').change(function(){
    $('span:contains('+ $(this).val() +')').effect('highlight', {color: 'red'}, 3000);
});

Change the color and 3000 as required.



来源:https://stackoverflow.com/questions/1804165/jquery-highlight-element-on-select-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!