Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery使用作为URL的ID选择项目_Javascript_Jquery_Checkbox_Jquery Selectors - Fatal编程技术网

Javascript jQuery使用作为URL的ID选择项目

Javascript jQuery使用作为URL的ID选择项目,javascript,jquery,checkbox,jquery-selectors,Javascript,Jquery,Checkbox,Jquery Selectors,我有一个div元素复选框,上面有一个URL,比如ID 身份证=http://www.mypage.com/page1 然后我得到一个变量,其中有链接 变量温度=http://www.mypage.com/page1 当我称之为: $+临时触发器“点击” 我得到了一个错误: 未捕获错误:语法错误,无法识别的表达式: 有什么想法吗?Aa@cristian如上所述,除非删除url中的所有特殊字符,否则不能将url用作id。您可以做的是,1从ID中删除spl字符,以便ID可用。2将jQuery数据设置为

我有一个div元素复选框,上面有一个URL,比如ID

身份证=http://www.mypage.com/page1

然后我得到一个变量,其中有链接

变量温度=http://www.mypage.com/page1

当我称之为:

$+临时触发器“点击”

我得到了一个错误:

未捕获错误:语法错误,无法识别的表达式:


有什么想法吗?

Aa@cristian如上所述,除非删除url中的所有特殊字符,否则不能将url用作id。您可以做的是,1从ID中删除spl字符,以便ID可用。2将jQuery数据设置为URL。这是一个虽然有点难看但有效的方法

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") );
});

必须转义所有特殊字符,如:或。。在这里阅读更多。这里的寓意是,你不应该使用url作为id。你可以使用一个更简单的id,如果你想传入url,你可以始终使用数据属性传入它,例如数据url=some_url。。。!这就是工作,谢谢。但是我正在将id值从一个url更改为一个普通id。这个更改是由脚本完成的,如果您真的想继续将url作为id。