Javascript 如果url包含#iphone,则创建链接

Javascript 如果url包含#iphone,则创建链接,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个根据所选选项生成链接的代码,但我希望它根据url中的字符串生成链接。如果url包含#iPhone,如何使用javascript中的字符串从url生成链接 以下是JSFIDLE: if(q1==document.location.href.indexOf('iPhone')>-1){ document.getElementById(“linkDiv”).innerHTML=“ "; } 要获取哈希标记值,请使用: if(window.location.hash.substr(1) == "

我有一个根据所选选项生成链接的代码,但我希望它根据url中的字符串生成链接。如果url包含#iPhone,如何使用javascript中的字符串从url生成链接

以下是JSFIDLE:

if(q1==document.location.href.indexOf('iPhone')>-1){
document.getElementById(“linkDiv”).innerHTML=“

"; }
要获取哈希标记值,请使用:

if(window.location.hash.substr(1) == "iPhone") {
//your code here
} else {
// Fragment doesn't exist
}
希望这有帮助

试试看:

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>";
}   
var url=window.location.hash;//检索当前哈希值(片段标识符)
如果(url.indexOf('iPhone')!=-1){//如果url的哈希包含“iPhone”,请执行以下操作
document.getElementById(“linkDiv”).innerHTML=“

"; }
编辑

<form name="quiz" id='quiz'>
    <div id="linkdiv">        
    </div>
</form>
 
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);
}   
从长远来看,使用jQuery就足够了:

HTML

<form name="quiz" id='quiz'>
    <div id="linkdiv">        
    </div>
</form>
 
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);
}   

jQuery

<form name="quiz" id='quiz'>
    <div id="linkdiv">        
    </div>
</form>
 
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);
}   
var url=”http://fairdash.com/?q1=Windowsphone&x=69&y=9";
var nextLink=“”;
var hash=window.location.hash;//检索当前哈希值(片段标识符)
if(hash.indexOf('iPhone')!=-1)
{
$(“#linkdiv”).append(nextLink);
}   

问题是什么?如果url中包含iPhone,如何使用javascript中的字符串从url生成链接@上面的Rajeshhimandoes代码给出了任何错误?@Rajeshhiman它从我这里不起作用为什么没有人在这些小提琴上尝试
console.log(window.location)
?你会发现你根本没有得到散列标签!只需创建一个旧的好的html文件,并以旧的方式在浏览器中进行测试。JSFIDLE并不能解决所有问题。我更新了JSFIDLE:|加载包含“iPhone”的页面时,按钮似乎没有出现。这是我的字符串?q1=ApplePhone&x=103&y=13您的查询字符串
?q1=ApplePhone&x=103&y=13
不包含
片段,这就是为什么它不加载按钮的原因。我包括了,?q1=Apple\iPhone&x=103&y=13,标签和按钮仍然没有出现。我在代码中也做了一些改进,但是我需要你的帮助来发现错误。你可以用所做的更改更新你的问题,以便我可以看一下吗?我大部分都有。当页面加载字符串:?q1=AppleiPhone&x=103&y=13?@user2680614
window.location.hash
就是在
@Skwal之后(包括)出现的,我在url中包括了一把小提琴和iphone,我仍然没有运气。我做错了什么@代码29我更新了你的代码,但仍然没有找到jsfiddle.net/SDq4P/13/#iphone