机管局@cristian上述
感谢,除非你删除所有特殊字符的形式,它不能使用URL作为ID。你可以做的是,1)从ID中删除spl字符,以便该ID可用。 2)将jQuery数据设置为您的URL。这是my work around虽然它有点丑陋,但它工作正常。
var temp = "http://www.mypage.com/page1";
//Create an id from 'temp' removing all spl chars.
var tempID = temp.replace(/[^a-z0-9-_]+/gi, "");
//Find all IDs starting with http
$("[id^='http']").filter(function() {
//Set data from the ID (URL)
$(this).data("ele-path", this.id);
//Change the id removing all spl chars.
this.id = this.id.replace(/[^a-z0-9-_]+/gi, "");
});
$(document).on("click", "#" + tempID, function() {
alert ("click");
//Get the URL from data
alert($(this).data("elePath"));
});
您必须转义所有特殊字符,如':'或'.'。在这里阅读更多http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css -符号/。这里的道德是,你不应该使用一个网址作为一个ID。 – cristian 2015-03-08 16:52:42
你可以使用一个简单的id,如果你想传递url,你总是可以通过data-url =“some_url”等数据属性传递它。 – 2015-03-08 17:05:01