开发者

Remove part of a string from a variable jquery

开发者 https://www.devze.com 2022-12-20 13:39 出处:网络
I\'m trying to take the id of a mouseover and strip out part of the ID, to leave m开发者_如何学Goe with just the core text I need to act on.

I'm trying to take the id of a mouseover and strip out part of the ID, to leave m开发者_如何学Goe with just the core text I need to act on.

My mouseover will return an id such as "nevadaActiveArea", but I need to manipulate that string down to just "nevada". All the searches I've run speak to how to do this on the contents of some element, but I just need the text in a variable. How do I achieve this?

Final code based on Josh Stodola's answer:

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


Given that all IDs end in "ActiveArea", you can do this using substring and indexOf...

$("#nevadaActiveArea").mouseover(function() {
  var id = $(this).attr("id");
  id = id.substring(0, id.indexOf("ActiveArea"));
  alert(id);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号