简体   繁体   English

Javascript / Jquery:如何修改此脚本以避免必须使用<A>标签?</a>

[英]Javascript/Jquery: How do I modify this script to avoid have to use a <A> tag?

One of the issues I have with this script is that I have to have a link tag to get the rel from it. 这个脚本的问题之一是,我必须具有一个链接标记才能从中获得rel I am looking at doing descriptions when user mouseover's a table row. 当用户将鼠标悬停在表行上时,我正在查看说明。

What I need: If TD has desc , append text. 我需要什么:如果TD具有desc ,请添加文本。 <td desc="blah blah blah">

 this.screenshotPreview = function () {
     xOffset = 20;
     yOffset = 30;

     $("tr:has(a[desc])").hover(function (e) {
         var text = $(this).find('a').attr('desc');
         $("body").append("<p id='screenshot'>" + text + "</p>");
         $("#screenshot")
             .css("top", (e.pageY - yOffset) + "px")
             .css("left", (e.pageX + xOffset) + "px")
             .fadeIn("fast");
     },function () {
         this.title = this.t;
         $("#screenshot").remove();
     });
     $("a.screenshot").mousemove(function (e) {
         $("#screenshot")
             .css("top", (e.pageY - yOffset) + "px")
             .css("left", (e.pageX + xOffset) + "px");
     });
 };


 // starting the script on page load
 $(document).ready(function () {
     screenshotPreview();
 });

JSFIDDLE: http://jsfiddle.net/zD99p/1/ (in jsfiddle, it still uses rel instead of desc . You can code it either way, I can change that with no problem) JSFIDDLE: http : //jsfiddle.net/zD99p/1/ (在jsfiddle中,它仍然使用rel而不是desc 。您可以用任何一种方式进行编码,我可以毫无问题地进行更改)

You can use data- attributes: 您可以使用data-属性:

http://jsfiddle.net/MWkXJ/ http://jsfiddle.net/MWkXJ/

<td data-desc="..."></td>

$("td[data-desc]").hover(function (e) {
     var text = $(this).data('desc');
     //...

Maybe something like this: 也许是这样的:

...
 $("td[desc]").hover(function (e) {
     var text = $(this).attr('desc');
     $("body").append("<p id='screenshot'>" + text + "</p>");
...

And change html to this: 并将html更改为此:

<td desc="blah blah">content ...

http://jsfiddle.net/6yMNM/ http://jsfiddle.net/6yMNM/

in your JSFIDDLE you missed a closing </a> . 在您的JSFIDDLE中,您错过了结束时间</a> I changed that and changed the <a> to a <div> and it worked. 我将其更改并将<a>更改为<div>并成功。 look here . 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免将 JavaScript ES6 模块导出添加到全局范围以用于普通脚本块? - How do I avoid adding JavaScript ES6 module exports to global scope for use in normal script blocks? 如何在脚本标签下使用url标签 - How do I use the url tag under script tag 如何使用PHP变量作为<script> tag when rendering jQuery code in my CakePHP view? - How do I use PHP variables as values for the <script> tag when rendering jQuery code in my CakePHP view? 如何修改此脚本以使用不同的 colors? - How do I modify this script to use different colors? 如何实现与“ Google跟踪代码管理器”结合使用的JavaScript“折扣兑现跟踪”脚本 - How do I implement a JavaScript “Discount Redemption Tracking” script for use with Google Tag Manager 如何使用JavaScript / jQuery修改:悬停样式? - how do I modify a :hover style using JavaScript / jQuery? 如何使用jQuery修改POST标题onclick? - How do I use jQuery to modify POST headers onclick? 如何使用Javascript修改节点的内容? - How do I use Javascript to modify the content of a node? 如何将 TypeScript 用于通过脚本标记加载的 JavaScript? - How can I use TypeScript for JavaScript loaded via script tag? 如何在a中正确转义内联Javascript <script> tag? - How do I properly escape inline Javascript in a <script> tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM