简体   繁体   中英

jQuery get text from element while trigger(“mouseenter”) on another element

How can i get the text from an element while triggering 'hover' action on another element?

This is an example: http://factory.jcrew.com/girls-clothing/swim/rashguards/PRDOVR~A3090/A3090.jsp?color_name=shocking-pink-ivory#

I want to get all colors, but the color names are displayed only when hovering the color box.

This is what i tried:

$("#color1 div[data-color='KS8866']").trigger("mouseenter",function() {
var color = $("span.color-name").text();
});

Thanks!

You probably need to use on() instead of trigger to bind the mouse enter event. The jQuery on Attach an event handler function for one or more events to the selected elements, jQuery Doc . The trigger Execute all handlers and behaviors attached to the matched elements for the given event type, jQuery doc . You have to have event handler first.

$("#color1 div[data-color='KS8866']").on("mouseenter",function() {
     var color = $("span.color-name").text();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM