[英]How to put an url inside a string variable in javascript?
如何在 javascript 的字符串變量中包含 url?
我能找到的資源都是關於從字符串中檢索 url,這不是我想要做的。
一些背景資料:
I'm trying to change the auto cursor with a javascript function to a custom defined cursor containing an url.
我的網頁默認的cursor是CSS中定義的自定義cursor,看起來像一個圓圈。 到目前為止,它工作正常。
body{url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;
}
使用 js function 將 cursor 從“自定義”更改為自動也可以:
document.body.style.cursor= "auto"
但是將 cursor 從自動更改回自定義不起作用,我認為因為字符串變量中有一個 url :
document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;"
將反斜杠放在括號、斜杠和冒號之前似乎不起作用,我已經嘗試過像這樣的 escape(url(...) 和 encodeURI ,但我不認為它是這樣工作的:
"url("+encodeURI(http://www.rw-designer.com/cursor-extern.php?id=14323))+"20 20,auto;"
任何幫助,將不勝感激
jquery 也歡迎回答。
您在style
值的 JavaScript cursor
字段中有一個額外的分號:
document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto;"
嘗試將其更改為:
document.body.style.cursor= "url(http://www.rw-designer.com/cursor-extern.php?id=14323) 20 20, auto"
當您通過 JavaScript 修改 CSS styles 時 - 您不應將分號放在屬性值的末尾
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.