问题描述
我希望脚本仅在选中文本时保存文本的值。 如果可能的话,我还想仅在单击它时将其值保存为变量,因此例如 名称:{此处为变量}#如果选中
<html>
<head>
<title>Feedback</title>
<script language="Javascript" >
function download(filename,text) {
var pom = document.createElement('a');
pom.setAttribute('href','data:text/plain;charset=utf-8,' +
encodeURIComponent(text));
pom.setAttribute('download',filename);
pom.style.display = 'none';
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
function addTextHTML()
{
document.addtext.name.value = document.addtext.name.value + ".html"
}
function addTextTXT()
{
document.addtext.name.value = document.addtext.name.value + ".txt"
}
</script>
</head>
<body>
<form name="addtext" onsubmit="download(this['name'].value,this['text'].value)">
<input type="checkBox" name="text" id="test" value=name>
<label for="test">Test</label>
<br>
<input type="text" name="name" value="" placeholder="File Name">
<input type="submit" onClick="addTextHTML();" value="Save As HTML">
<input type="submit" onClick="addTexttxt();" value="Save As TXT">
</form>
</body>
</html>
解决方法
尝试使用break;
。但是它将给出错误,您将可以在控制台上看到它。
所以尝试这样的事情;
var text = "";
var a;
for (a = 1; a < 2; a++){
//code will be here which gets in the loop
text += "Working";
}
document.getElementById("demo").innerHTML = text;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width = device-width,initial-scale = 1.0">
<meta http-equiv="X-UA-Compatible" content = "ie=edge">
<title> Keat </title>
</head>
<body>
<p id="demo"></p>
</body>
</html>
这将使循环进行,但会限制循环。