我正在为商店的产品页面工作,能够预览多个图像。 截至目前,正在查看的当前图像更新了URL上的#hashtag。 我有一个选择列表,每个选项都标有一个匹配每个潜在主题标签的类。
我有一个开始,但只是不太确定如何使用jQuery来获取主题标签,将其与类匹配并执行某些操作。 触发点击或只是将选项更新为“已选择”会更好吗?
这是我的开始,不太清楚为什么它不起作用。
https://jsfiddle.net/ykmgzyc7/
var url = window.location.href; var hash = url.substring(url.indexOf('#')); $('option.' + hash).trigger('click');I'm working on a product page for a store with the ability to preview multiple images. As of right now the current image being viewed updates the # hashtag on the URL. I have a select list with each option that is tagged with a class matching each potential hashtag.
I got a start, but just not quite sure how to use jQuery to take the hashtag, match it with a class and do something. Would it be better to trigger a click or simply update the option to 'selected'?
Here is my start, not quite sure why it's not working.
https://jsfiddle.net/ykmgzyc7/
var url = window.location.href; var hash = url.substring(url.indexOf('#')); $('option.' + hash).trigger('click');最满意答案
由于您实际上并未加载新页面而只是使用锚点,因此可以使用jquery动态修改选择。
HTML:
<ul> <li><a href="#one">One</a></li> <li><a href="#two">Two</a></li> <li><a href="#three">Three</a></li> </ul> <select id="selectOptions"> <option class="one">One</option> <option class="two">Two</option> <option class="three">Three</option> </select>jQuery的:
$("a").on("click",function(){ var link=$(this).attr('href'); var hash = link.substring(link.indexOf('#')+1); $('#selectOptions option').removeAttr('selected'); $('#selectOptions .'+hash).attr('selected',true); });https://jsfiddle.net/ykmgzyc7/3/
Since you aren't actually loading a new page and just using the anchors you can dynamically modify the selection with jquery.
HTML:
<ul> <li><a href="#one">One</a></li> <li><a href="#two">Two</a></li> <li><a href="#three">Three</a></li> </ul> <select id="selectOptions"> <option class="one">One</option> <option class="two">Two</option> <option class="three">Three</option> </select>Jquery:
$("a").on("click",function(){ var link=$(this).attr('href'); var hash = link.substring(link.indexOf('#')+1); $('#selectOptions option').removeAttr('selected'); $('#selectOptions .'+hash).attr('selected',true); });https://jsfiddle.net/ykmgzyc7/3/
更多推荐
hashtag,URL,hash,更新,电脑培训,计算机培训,IT培训"/> <meta name="descripti
发布评论