简体   繁体   English

在href标签中的onMouseOver时,如何在jQuery中分配动态元素ID?

[英]How to assign dynamic element id in jQuery when onMouseOver in a href tag?

I have 3 hyperlinks and DIV randomly generated by PHP script with dynamic ids. 我有3个超链接和由具有动态ID的PHP脚本随机生成的DIV。 For example, 例如,

<a href="http://www.example.com/find.php?id=xy1" id="xy1" onMouseover="analyze('final', 'xy1')">xy1</a>
<a href="http://www.example.com/find.php?id=ay2" id="ay2" onMouseover="analyze('final', 'ay2')">ay2</a>
<a href="http://www.example.com/find.php?id=am3" id="am3" onMouseover="analyze('final', 'am3')">am3</a>

<div id="xy1"></div>
<div id="ay2"></div>
<div id="am3"></div>

I want to display some text based on the onMouseover event through a function analyze() in jQuery. 我想通过jQuery中的函数onMouseover analyze()显示基于onMouseover事件的一些文本。 For example, 例如,

function analyze(db, target) {
  jQuery.ajax({
    type: "GET",
    url:  "toThumb.php",
    data: 'db=' + db + '&id=' + target,
    success: function(output){ $("#"+target).html(output); }
  });
}

Here, the element id #xy1 must be automatically assigned based on onMouseover event. 在这里,元素ID #xy1必须自动根据分配onMouseover事件。 Like xy1 as a value of a variable name. xy1作为变量名的值。 Maybe on the next onMouseover event it will be ay2 or am3 . 也许在接下来onMouseover事件将是AY2AM3。

I tried to set with $("#"+target).html(output); 我试图设置$("#"+target).html(output); It failed... If I test individually with $("#xy1").html(output); 失败了...如果我分别用$("#xy1").html(output); OR $("#ay2").html(output); $("#ay2").html(output); OR $("#am3").html(output); $("#am3").html(output); It works fine. 工作正常。

Does my way of coding is wrong? 我的编码方式是否错误? Or, Can I use this instead of passing parameters? 或者,我可以使用this代替传递参数吗?

You can use .data() 您可以使用.data()

In your html you have to pass your id in data-target attribute like this. 在您的html中,您必须像这样在data-target属性中传递ID。

Example:- 例:-

<a href="http://www.example.com/find.php?id=xy1" data-target="xy1" onMouseover="analyze(this)">xy1</a>
<a href="http://www.example.com/find.php?id=ay2" data-target="ay2" onMouseover="analyze(this)">ay2</a>
<a href="http://www.example.com/find.php?id=am3" data-target="am3" onMouseover="analyze(this)">am3</a>

<div id="xy1"></div>
<div id="ay2"></div>
<div id="am3"></div>

Then in your jquery you can get data value. 然后在您的jquery中,您可以获得数据值。 and pass your data based on this data value. 并根据此数据值传递您的数据。

function analyze(this)
{
    var getTargetId = $(this).data("target");
    $("#"+getTargetId).html(data); 
}

First of all id should be unique in DOM. 首先,id在DOM中应该是唯一的。 And in place of it you can use class as below. 代替它,您可以使用下面的类。

Please check below working demo. 请检查下面的工作演示。

 function analyze(ele){ $("."+ele.id).html(ele.innerHTML); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="http://www.example.com/find.php?id=xy1" id="xy1" onMouseover="analyze(this)">xy1</a> <a href="http://www.example.com/find.php?id=ay2" id="ay2" onMouseover="analyze(this)">ay2</a> <a href="http://www.example.com/find.php?id=am3" id="am3" onMouseover="analyze(this)">am3</a> <div class="xy1"></div> <div class="ay2"></div> <div class="am3"></div> 

The issue is due to duplicate IDs within the document. 该问题是由于文档中的ID重复造成的。 According to W3C : 根据W3C

id = ID

A unique identifier for the element. 元素的唯一标识符。 There must not be multiple elements in a document that have the same id value. 文档中不得有多个具有相同ID值的元素。

Also, there is no sense in displaying the items in different containers, as you might simply generate them with PHP as well. 另外,将项目显示在不同的容器中也是没有意义的,因为您也可以简单地使用PHP生成它们。

 function analyze(node) { $('#container').html(node.innerHTML /* whatever */); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container">(empty)</div> <a href="http://www.example.com/find.php?id=xy1" id="xy1" onmouseover="analyze(this)">xy1</a> <a href="http://www.example.com/find.php?id=ay2" id="ay2" onmouseover="analyze(this)">ay2</a> <a href="http://www.example.com/find.php?id=am3" id="am3" onmouseover="analyze(this)">am3</a> 

Thanks to Rahul Patel . 感谢Rahul Patel I did three changes in my coding. 我在编码中做了三处更改。

(1) Removed id (1)移除id

<a href="http://www.example.com/find.php?id=xy1" onMouseover="analyze('final', 'xy1')">xy1</a>
<a href="http://www.example.com/find.php?id=ay2" onMouseover="analyze('final', 'ay2')">ay2</a>
<a href="http://www.example.com/find.php?id=am3" onMouseover="analyze('final', 'am3')">am3</a>

(2) Changed id to class (2)将id更改为class

<div class="xy1"></div>
<div class="ay2"></div>
<div class="am3"></div>

(3) Changed $("#"+target).html(output); (3)更改了$("#"+target).html(output); to $("."+target).html(output); $("."+target).html(output);

Now problem solved... 现在问题解决了...

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

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