Javascript 如何使用jquery在鼠标上方查找当前元素文本

Javascript 如何使用jquery在鼠标上方查找当前元素文本,javascript,jquery,Javascript,Jquery,这是我的密码 <!DOCTYPE html> <html> <body> <svg id="a" height="210" width="400"> <path id="b" d="M150 0 L75 200 L225 200 Z" /> </svg> </body> </html> 当鼠标从b移到b上时,我想输入该代码(path id=“b”d=“M150 0 L75 200 L22

这是我的密码

<!DOCTYPE html>
<html>
<body>

<svg id="a" height="210" width="400">
  <path id="b" d="M150 0 L75 200 L225 200 Z" />
</svg>

</body>
</html>

当鼠标从b移到b上时,我想输入该代码(path id=“b”d=“M150 0 L75 200 L225 200 Z”)。如何使用jquery获取该代码?

您可以使用:

正如所指出的,这在IE中不起作用,因为它不遵循。您可以通过克隆
元素,将其附加到HTML正文以使其成为DOM的一部分,然后从那里获取呈现的HTML来解决此问题。
注意:这不是HTML的精确表示,因为它与上下文无关。例如,它包含
xmlns
,但由于它是一个jQuery对象,您可以随意修改它:

$("#b").hover(function() {
    // Create a clone of the <path>
    var clone = $(this).clone();
    // Append it to the <body>
    clone.appendTo("body");
    // Wrap it in a containing <div> ... you'll see why in a minute
    clone.wrap("<div>");
    // Now we grab the innerHTML of that containing <div> - outerHTML won't work
    console.log(clone.parent().html());
    // Prints: <path xmlns="http://www.w3.org/2000/svg" id="b" d="M 150 0 L 75 200 L 225 200 Z" /> 

    // Now remove our temporary element again...
    clone.parent().remove();
});
$(“#b”).悬停(函数(){
//创建
var clone=$(this.clone();
//将其附加到
克隆。附体(“身体”);
//把它包在一个容器里……你马上就会明白为什么了
clone.wrap(“”);
//现在我们抓取包含-outerHTML的innerHTML将不起作用
log(clone.parent().html());
//印刷品:
//现在再次移除我们的临时元素。。。
clone.parent().remove();
});
您可以使用:

正如所指出的,这在IE中不起作用,因为它不遵循。您可以通过克隆
元素,将其附加到HTML正文以使其成为DOM的一部分,然后从那里获取呈现的HTML来解决此问题。
注意:这不是HTML的精确表示,因为它与上下文无关。例如,它包含
xmlns
,但由于它是一个jQuery对象,您可以随意修改它:

$("#b").hover(function() {
    // Create a clone of the <path>
    var clone = $(this).clone();
    // Append it to the <body>
    clone.appendTo("body");
    // Wrap it in a containing <div> ... you'll see why in a minute
    clone.wrap("<div>");
    // Now we grab the innerHTML of that containing <div> - outerHTML won't work
    console.log(clone.parent().html());
    // Prints: <path xmlns="http://www.w3.org/2000/svg" id="b" d="M 150 0 L 75 200 L 225 200 Z" /> 

    // Now remove our temporary element again...
    clone.parent().remove();
});
$(“#b”).悬停(函数(){
//创建
var clone=$(this.clone();
//将其附加到
克隆。附体(“身体”);
//把它包在一个容器里……你马上就会明白为什么了
clone.wrap(“”);
//现在我们抓取包含-outerHTML的innerHTML将不起作用
log(clone.parent().html());
//印刷品:
//现在再次移除我们的临时元素。。。
clone.parent().remove();
});

这在IE中不起作用。。你能办理登机手续吗IE@AkbarBasha典型的IE:)我用另一个选项更新了答案这在IE中不起作用。。你能办理登机手续吗IE@AkbarBasha典型的IE:)我用另一个选项更新了答案
$("#b").hover(function() {
    // Create a clone of the <path>
    var clone = $(this).clone();
    // Append it to the <body>
    clone.appendTo("body");
    // Wrap it in a containing <div> ... you'll see why in a minute
    clone.wrap("<div>");
    // Now we grab the innerHTML of that containing <div> - outerHTML won't work
    console.log(clone.parent().html());
    // Prints: <path xmlns="http://www.w3.org/2000/svg" id="b" d="M 150 0 L 75 200 L 225 200 Z" /> 

    // Now remove our temporary element again...
    clone.parent().remove();
});