我有一个代码根据选择的选项生成链接但我希望它根据网址中的字符串生成链接。如果它包含#iPhone,如何使用javascript中的字符串生成链接?
这是JSFiddle:http://jsfiddle.net/SDq4P/12/#iphone
if (q1 == document.location.href.indexOf('iPhone') > -1 ) {
document.getElementById("linkDiv").innerHTML = "<BR/><BR/>
<img id=create style=cursor:pointer; cursor:hand; type=img
value=Next src='http://fairdash.com/Next-Button.png'
href='http://fairdash.com'
onclick=\"location.href = $(this).attr('href')+'?
q1=Windowsphone&x=69&y=9';return false\"></img>";
}
答案 0 :(得分:3)
要获取哈希标记值,请使用:
if(window.location.hash.substr(1) == "iPhone") {
//your code here
} else {
// Fragment doesn't exist
}
希望这有帮助!
答案 1 :(得分:1)
尝试:
var url = window.location.hash; // retrieve current hash value (fragment identifier)
if(url.indexOf('iPhone') != -1){ // do the following if URL's hash contains "iPhone"
document.getElementById("linkDiv").innerHTML = "<BR/><BR/>
<img id=create style=cursor:pointer; cursor:hand; type=img
value=Next src='http://fairdash.com/Next-Button.png'
href='http://fairdash.com'
onclick=\"location.href = $(this).attr('href')+'?
q1=Windowsphone&x=69&y=9';return false\"></img>";
}
修改强>:
从discussions开始,这应该足够了,使用jQuery:
<强> HTML 强>:
<form name="quiz" id='quiz'>
<div id="linkdiv">
</div>
</form>
<强>的jQuery 强>:
var url = "http://fairdash.com/?q1=Windowsphone&x=69&y=9";
var nextLink = "<a href='" + url + "' class='select' style='cursor:pointer; cursor:hand;'>Next</a>";
var hash = window.location.hash; // retrieve current hash value (fragment identifier)
if (hash.indexOf('iPhone') != -1)
{
$("#linkdiv").append(nextLink);
}