2014-02-25 55 views
-1

我有一個代碼生成鏈接取決於選擇的選擇,但我希望它生成一個基於URL中的字符串的鏈接。如果包含#iPhone,我如何使用url中的字符串生成鏈接,並使用javascript中的字符串?如果url包含#iphone然後使鏈接

這裏的的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

有什麼問題嗎? –

+0

如果包含#iPhone,我該如何使用url中的字符串生成一個帶有javascript字符串的鏈接? @RajeshDhiman – user2680614

+0

上面的代碼是否給出了錯誤? –

回答

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); 
} 
+0

我更新了JSFiddle:http:// jsfiddle。 net/SDq4P/4/|加載包含'iPhone'的頁面時,該按鈕似乎不會顯示。這是我的字符串?q1 = AppleiPhone&x = 103&y = 13 – user2680614

+0

您的查詢字符串'?q1 = AppleiPhone&x = 103&y = 13'不包含'#'片段,這就是爲什麼它不加載按鈕。 – chridam

+0

我包括了,?q1 = Apple#iPhone&x = 103&y = 13,哈希標籤和按鈕仍然沒有出現。 – user2680614

3

爲了獲取散列標籤值使用:

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

希望這有助於!

+0

我主要是http://jsfiddle.net/SDq4P/4/。是否有任何原因,當頁面加載字符串時按鈕不出現:?q1 = AppleiPhone&x = 103&y = 13? – user2680614

+0

@ user2680614'window.location.hash'正是('包括)'#' – Skwal

+0

@Skwal後面包含的url中包含#iphone的小提琴,我仍然沒有運氣。我究竟做錯了什麼? http://jsfiddle.net/SDq4P/13/#iphone – user2680614

相關問題