我正在尝试获取鼠标悬停的 ID 并删除 ID 的一部分,只留下我需要执行操作的核心文本。
我的鼠标悬停将返回一个 ID,例如“nevadaActiveArea”,但我需要将该字符串操作为“nevada”。我运行的所有搜索都说明了如何对某些元素的内容执行此操作,但我只需要变量中的文本。我如何实现这一目标?
基于 Josh Stodola 答案的最终代码:
$("area").mouseover(function(){
var overID = $(this).attr("id");
if(overID.indexOf("ActiveArea") >= 1){
id = overID.substring(0, overID.indexOf("ActiveArea"));
}else if(overID.indexOf("Hotspot") >= 1){
id = overID.substring(0, overID.indexOf("Hotspot"));
}
$("#"+id).show();
});
最佳答案
鉴于所有 ID 都以“ActiveArea”结尾,您可以使用 substring 和 indexOf 来完成此操作...
$("#nevadaActiveArea").mouseover(function() {
var id = $(this).attr("id");
id = id.substring(0, id.indexOf("ActiveArea"));
alert(id);
});
关于jquery - 从变量 jquery 中删除字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2298483/