[英]Create a recyclable function and converting a string to a variable name
I'm trying to make a check box to show/hide a HTML element, but I am trying to make it as efficient as possible (recycling one function and enabling it to evolve, rather than manually and statically writing unique functions). 我试图创建一个复选框来显示/隐藏HTML元素,但我试图使其尽可能地高效(回收一个功能并使其得以发展,而不是手动和静态地编写独特的功能)。
I have already succeeded at this once, but in a slightly different scenario (included: "JavaScript Extract - Working Recycled Function") but cannot get it to work in this new scenario (included: "JavaScript Extract - Not Working Recycled Function). I have scavenged what I can from it but I am having difficultly this time around. My brain has kinda ground to a halt after a couple of hours to solving other problems. I have multiple elements waiting to also use this, but for now I'm just trying to get it to work with the Google element first. 我已经成功完成了一次,但是在一个稍微不同的场景中(包括:“ JavaScript提取-循环功能正常工作”),但是无法使其在新场景下工作(包括:“ JavaScript提取-无效的循环功能”工作)。已经清除了我所能提供的信息,但这次却遇到了困难。几个小时后,我的大脑就停滞了下来,以解决其他问题。我有多个要素也在等待使用,但现在只是尝试使其首先与Google元素一起使用。
HTML Extract HTML提取
<div id="googleContainer">
<input type="text" id="googleSearchBox" title="Google Search" class="searchBox" />
<input type="button" id="googleSearchButton" onClick="execSearchWithClick(this.id)" />
</div>
<input type="checkbox" id="googleCheck" checked="checked" onClick="setPref(this.id)"/>
JavaScript Extract - Working Recycled Function JavaScript提取-回收功能
var googleHome = "http://www.google.com/"; //Set home page
var googleSearchURL = googleHome + "search?q="; //Prime search URL
function execSearchWithClick(id){
alert(id) //Check ID
var searchBox = document.getElementById(id.replace("Button", "Box")); //Convert "googleSearchButton" to "googleSearchBox" to match target element
var searchQuery = searchBox.value; //Extract value from converted string-to-variable
if (searchQuery == ""){
if (id == "googleSearchButton") {
window.open(googleHome);
} //I would use "if(condition && condition)" rather than nested IFs here, but I plan to use ELSE IF very soon
} else {
if (id == "googleSearchButton") {
window.open(googleSearchURL + searchQuery);
}
}
}
JavaScript Extract - Not Working Recycled Function JavaScript提取-无法正常使用的回收功能
var googleDisplay = true;
function setPref(id){
alert(id) //Check ID
var checkboxDisplayString = document.getElementById(id.replace("Check", "Display")); //Convert "googleCheck" to "googleDisplay" to match boolean variable
alert(checkboxDisplayString) //Null value reply
var checkboxDisplayVar = checkboxDisplayString //Convert (what I suspect to be) String to variable name
alert(checkboxDisplayVar) //Null value reply
if (id == "googleCheck" && checkboxDisplayVar == true){
checkboxDisplayVar = false;
applyPref();
} else {
checkboxDisplayVar = true;
applyPref();
}
}
function applyPref(){
if (googleDisplay == true){
document.getElementById("googleContainer").style.display = "block";
} else {
document.getElementById("googleContainer").style.display = "none";
}
}
EDIT: I've added much more detail for those who asked now. 编辑:我为那些现在问的人增加了更多细节。 Sorry if everything has become more confusing. 很抱歉,如果一切都变得更加混乱。
This would be my attempt: 这是我的尝试:
function setPref(id){
var checkbox = document.getElementById(id);
applyPref(checkbox.checked);
}
function applyPref(show){
if (show){
document.getElementById("googleContainer").style.display = "block";
} else {
document.getElementById("googleContainer").style.display = "none";
}
}
I may be missing something, but I think that something like the above should work 我可能会遗漏一些东西,但是我认为类似上面的东西应该可以工作
I would do something like this.. instead of converting variable names, just pass the variables to your function onCLick. 我会做这样的事情..而不是转换变量名称,只需将变量传递给函数onCLick。
<div id="one" style="width:100%;height:100px;background-color:#000;"></div>
<form>
<input id="one-check" type="checkbox" name="one" onclick="showOrHide('one');">
</form>
<div id="two" style="width:100%;height:100px;background-color:#000;"></div>
<form>
<input id="two-check" type="checkbox" name="two" onclick="showOrHide('two');">
</form>
JAVASCRIPT JAVASCRIPT
function showOrHide(id){
alert(id);
var checkBox = document.getElementById(id+'-check');
if (checkBox.checked == 1){
document.getElementById(id).style.display = "none";
} else {
document.getElementById(id).style.display = "block";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.