我第一次使用 dat.gui 为我感兴趣的电视节目创建搜索系统。我首先创建了一个对象,其中节目标题作为键,搜索查询作为值。一切都很好,直到我将其付诸实践,并且每次都搜索最后一个密钥。当我这样做时,我担心会发生这种情况,但无论如何都想不到。我考虑过使用window['string']
从字符串中设置变量名,但无法弄清楚它在我的情况下是如何工作的。
我正在使用用户脚本,所以我有一些限制。我的脚本如下:
NodeList.prototype.forEach = Array.prototype.forEach;
var shows = {
'Agents of S.H.I.E.L.D.' : 'marvels agents of s.h.i.e.l.d.',
'Arrow' : 'arrow s',
'Brooklyn Nine-Nine' : 'brooklyn nine-nine',
'Castle' : 'castle 2009',
'Chicago Fire' : 'chicago fire',
'Chicago P.D.' : 'chicago p.d.',
'Constantine' : 'contanstine s',
'Doctor Who' : 'doctor who',
'Gotham' : 'gotham s',
'Grimm' : 'grimm s',
'Madam Secretary' : 'madam secretary',
'NCIS LA' : 'ncis los angeles',
'Resurrection' : 'resurrection',
'Saturday Night Live' : 'saturday night live',
'Scandal' : 'scandal s',
'Scorpion' : 'scorpion s',
'Stalker' : 'stalker s',
'The 100' : 'the 100 s',
'The Big Bang Theory' : 'the big bang theory',
'The Blacklist' : 'the blacklist',
'The Flash' : 'the flash',
'The Tonight Show Starring Jimmy Fallon' : 'jimmy fallon',
'Z Nation' : 'z nation'
},
show = function() {
for (var k in shows) {
this[k] = function() {
document.getElementById('tsstac').value = shows[k].replace(/ /g, '.');
document.querySelectorAll('[type=submit]')[0].click();
};
}
};
window.onload = function() {
var text = new show(),
gui = new dat.GUI({
resizable : false,
hideable : false
});
for (var k in shows) {
gui.add(text, k);
}
document.querySelectorAll('li.cr>div>span.property-name').forEach(function(v) {
v.style.width = '100%';
});
};
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<input type="text" id="tsstac" />
<input type="submit" />
<p>Regardless of which button is pressed, it'll always be the last query.</p>
无论按下什么按钮,都会搜索最后一个查询(Z Nation)。我该如何解决这个问题?
编辑:
我已经更新了代码,但仍然无法正常工作。
编辑:
代码不再创建复选框,而是创建按钮列表。问题依然存在,现在进行演示。