如何通过单击Javascript中的按钮跳转到另一个页面?

如何通过单击Javascript中的按钮跳转到另一个页面?,javascript,html,Javascript,Html,我将通过单击按钮重定向我的页面。但我完全被这些引语搞砸了 代码如下: child.innerHTML += "<a class='sliding-element' href='javascript:sendSurvey(" + (m + 1) + ")'>Survey " + surveyList[m].Name + "</a>" + "<input type='button' onClick='parent.location='QuestionPage.aspx'

我将通过单击按钮重定向我的页面。但我完全被这些引语搞砸了

代码如下:

child.innerHTML += "<a class='sliding-element' href='javascript:sendSurvey(" + (m + 1) + ")'>Survey " + surveyList[m].Name + "</a>" + "<input type='button' onClick='parent.location='QuestionPage.aspx''/>";
child.innerHTML++;
它无法将我重定向到QuestionPage.aspx。它什么也不做。

child.innerHTML++;
child.innerHTML += "<a class='sliding-element' href='javascript:sendSurvey(" + (m + 1) + ")'>Survey " + surveyList[m].Name + "</a>" + "<input type='button' onClick=\"parent.location='QuestionPage.aspx'\"/>";

您的引号有问题:您需要使用“转义”而不是嵌套“

我不知道您的函数做什么,但您可以尝试转义双引号:

child.innerHTML += "<a class=\"sliding-element\" href=\"javascript:sendSurvey(\" + (m + 1) + \")\">Survey " + surveyList[m].Name + "</a>" + "<input type=\"button\" onClick="parent.location=\"QuestionPage.aspx\""/>";
child.innerHTML++;

以下是您的代码:

child.innerHTML += "<a class='sliding-element'"
                +  "href='javascript:sendSurvey(" + (m + 1) + ")'>Survey "
                +   surveyList[m].Name + "</a>"
                +  "<input type='button' "
                +  "onClick='parent.location='QuestionPage.aspx''/>";

不要构建HTML,构建元素:

var link = document.createElement("a");
link.className = "sliding-element";
link.href = "";
link.onclick = function (e) {
    e = e || window.event;
    e.preventDefault && e.preventDefault();
    e.returnValue = false;
    sendSurvey(m + 1);
};
link.appendChild(document.createTextNode("Survey " + surveyList[m].Name));
var btn = document.createElement("input");
btn.type = "button";
btn.value = "click me";
btn.onclick = function (e) {
    parent.location.href = "QuestionPage.aspx";
};
child.appendChild(link);
child.appendChild(btn);

它有点冗长,但不太容易出错。

为什么不能使用标准链接并将其样式设置为按钮?或者如果您非常想使用浏览器输入按钮,只需将其包装在标准链接中?生成的HTML链接看起来是什么样的?这不是真的。编写
window.location=http://www.google.com“;
在您的F中要查看的是irebug控制台。而不是parent.location,它不应该是window.location或document.location,无论浏览器支持哪种浏览器,此时父对象都会引用它的DOM父对象。@Bipins:也许,不确定。我只是修复引号,而不是更改他的脚本的语义或检查它的优点。@Juan:你只是为我做的!@Bipins-请参见。您可能正在考虑
this.parentNode
document.createElement(“”
还有,为什么
e.preventDefault&&e.preventDefault()
?列出了该语法。我不确定现代浏览器是否仍然如此,但至少在过去,您无法指定
输入的
类型,因此需要允许属性的语法。
&
if(例如默认)的缩写e.preventDefault()
。您可以使用
setAttribute()
设置类型,我认为
类型
属性不起作用。另外,您对后者的看法是正确的,我没有注意到
()
LHS上缺少:)我刚刚测试了它。只要在元素添加到dom之前设置了type属性,它就可以工作。这并不能真正解释为什么我脑子里想你必须将类型传递给
createElement
。也许这是IE4的问题,但我不会为了测试这个理论而拖出一台旧的386 PC。我更喜欢显式属性赋值-我会在最后更新我的答案来做
appendChild
。你在IE7中试过吗?这就是我记得
类型
属性失败的地方,但可能只是我的记忆失败了:PYes,因为他们的答案不正确。这就是向下投票的意思。现在被接受了答案有一个明显的语法错误,即使是加亮标记的语法也会使错误变得明显。@TomalakGeret'kal:我保留对误导性答案的否决票,而不是对小错误的否决票,在这种情况下,我宁愿发表评论。@Juan:问题是“请修正我的引用”。这个答案会让问题变得更糟。这不是一个小错误。
var link = document.createElement("a");
link.className = "sliding-element";
link.href = "";
link.onclick = function (e) {
    e = e || window.event;
    e.preventDefault && e.preventDefault();
    e.returnValue = false;
    sendSurvey(m + 1);
};
link.appendChild(document.createTextNode("Survey " + surveyList[m].Name));
var btn = document.createElement("input");
btn.type = "button";
btn.value = "click me";
btn.onclick = function (e) {
    parent.location.href = "QuestionPage.aspx";
};
child.appendChild(link);
child.appendChild(btn);