javascript - 图像上的鼠标光标坐标 : <br/> not working

标签 javascript jquery

感谢我收到的建议here ,我清理了我的代码,现在使用纯 jQuery。我对发现的脚本做了轻微编辑 here我得到了这个:

window.onload = function(){

    var tooltip = $( '<div id="tooltip">' ).appendTo( 'body' )[0];

$( 'img' ).
    each(function () {
        var pos = $( this ).position(),
            top = pos.top,
            left = pos.left,
            width = $( this ).width(),
            height = $( this ).height(); 
        $( this ).
            mousemove(function ( e ) {
                var x = e.pageX - left,
                    y = e.pageY - top;

                $( tooltip ).text( 'x = ' + x + ', y = ' + y ).css({
                    left: e.clientX + 10,
                    top: e.clientY + 10
                }).show();
            }).
            mouseleave(function () {
                $( tooltip ).hide();
            }); 

    });

};

我有一个问题:我尝试将一行代码更改为:

$( tooltip ).text( 'x = ' + x + '<br/> y = ' + y ).css({

但是它不起作用。我没有打破界限,而是得到了这个:

line break not working

最佳答案

更改:

$( tooltip ).text( 'x = ' + x + '<br/> y = ' + y ).css({

至:

$( tooltip ).html( 'x = ' + x + '<br/> y = ' + y ).css({

关于javascript - 图像上的鼠标光标坐标 : <br/> not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245250/

相关文章:

javascript - 使用 JS/jQuery 获取对象在数组中的位置

javascript - AngularJS,指令元素为空

Javascript:点击获取并在剪贴板上分配值

javascript - 选择子元素(输入字段)时更改父 div 的 CSS

php - 使用 jquery ajax 处理服务器端验证的最佳方法

javascript - 如何将复选框列表的选定值保存在数组中?

javascript - Rails 4 中的 float 入门

javascript - 未捕获的语法错误 : missing ) after argument list in my code

javascript - 新的 Chrome 行为 : clicked element stays where it is and document scroll position moves when new content loaded

javascript - 带有 URL 片段问题的 IE 中的奇怪 document.location.href 问题