gpt4 book ai didi

javascript - 根据哈希 url 参数在页面加载时加载内容

转载 作者:行者123 更新时间:2023-11-28 06:26:50 25 4
gpt4 key购买 nike

Jquery/Javascript 新手试图了解如何根据多个 window.location.hash 参数在页面上加载内容。

我想知道我是否可以创建如下的 if 语句:

    if (window.location.hash){          
$('li[data-style"' + style + '"]', '#options > ul').trigger('click');
}

else {
$('li:first', '#options > ul').trigger('click');
}

else 语句有效,但我无法使 if 语句起作用。完整代码如下

<script>
(function($) {

var urlprops = {};

$('#options > ul').on('click', 'li', function() {

var $this = $(this).addClass('selected'),
imgId = $this.parent('ul').data('img'),
img = $(imgId),
cat = $this.data('cat'),
style = $this.data('style'),
desc = $this.text();

$this.siblings().removeClass('selected')

img.attr('src', '/' + cat + '/' + style + '.png').attr('alt', 'tool ' + desc);

urlprops[cat] = style;

buildURL();

});

function buildURL() {


var combinedProps = $.param(urlprops),
baseUrl = window.location.href,
finalUrl = baseUrl + '#' + combinedProps;

$(location).attr('hash', combinedProps);

}

if (window.location.hash){
$('li[data-style"' + style + '"]', '#options > ul').trigger('click');
}

else {
$('li:first', '#options > ul').trigger('click');
}

})(jQuery);

</script>
.selected {
background: red;
}
#url {
color: blue;
font-size: 11px;
}
<h3>
URL = <span id="url"></span>
</h3>
<h3>
images
</h3>
<img id="wallImg" src="/testpath/selwall/nopaint.jpg" />
<img id="doorImg" src="/testpath/selwall/nopaint.jpg">
<img id="handleImg" src="/testpath/selwall/nopaint.jpg">
<img id="topImg" src="/testpath/selwall/nopaint.jpg">
<img id="floorImg" src="/testpath/selwall/nopaint.jpg">


<h3>
options
</h3>

<div id="options">

<ul class="selDoor" data-img="#doorImg">
<li data-cat="door" data-style="white">White Door</li>
<li data-cat="door" data-style="red">Red Door</li>
<li data-cat="door" data-style="yellow">Yellow Door</li>
</ul>

<ul class="selHandle" data-img="#handleImg">
<li data-cat="handle" data-style="round">Round Knob</li>
<li data-cat="handle" data-style="cup">Cup Handle</li>
<li data-cat="handle" data-style="bar">Bar Handle</li>
</ul>

<ul class="selTop" data-img="#topImg">
<li data-cat="top" data-style="wood">Wood Top</li>
<li data-cat="top" data-style="plastic">Plastic top</li>
<li data-cat="top" data-style="stone">Stone top</li>
</ul>

<ul class="selFloor" data-img="#floorImg">
<li data-cat="floor" data-style="wood">Wood Floor</li>
<li data-cat="floor" data-style="tile">Tile Floor</li>
<li data-cat="floor" data-style="laminate">Laminate Floor</li>
</ul>

<ul class="selWall" data-img="#wallImg">
<li data-cat="wall" data-style="bluepaint">Blue Walls</li>
<li data-cat="wall" data-style="redpaint">Red Walls</li>
<li data-cat="wall" data-style="greenpaint">Green Walls</li>
</ul>

</div>

如能就我的问题提出任何建议,我们将不胜感激。

最佳答案

您在选择器中忘记了 =:

$('li[data-style="' + style + '"]', '#options > ul')

关于javascript - 根据哈希 url 参数在页面加载时加载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com