Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅使用javascript(无jquery)在鼠标悬停上获取html的id_Javascript_Html - Fatal编程技术网

仅使用javascript(无jquery)在鼠标悬停上获取html的id

仅使用javascript(无jquery)在鼠标悬停上获取html的id,javascript,html,Javascript,Html,我的问题是,如果我有一个文本html元素,看起来像 <a id='1' onmouseover="changeImage('setname/setnumber')">Cardname</a> 其中node是卡片列表中的位置,并且由于我开始使用的文档的格式,它按名称的字母顺序包含每张卡片,而不是在集合中按逻辑编号 Card.prototype.toLink = function() { var txt = ""; this.number; if (

我的问题是,如果我有一个文本html元素,看起来像

<a id='1' onmouseover="changeImage('setname/setnumber')">Cardname</a>
其中node是卡片列表中的位置,并且由于我开始使用的文档的格式,它按名称的字母顺序包含每张卡片,而不是在集合中按逻辑编号

Card.prototype.toLink = function()
{
    var txt = "";
    this.number;
    if (this.promo == 'false')
    {
    var image = this.set.replace(/ /g, "_") + '/' + this.number;
    txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + "')>";
    txt += this.toString() + "</" + "a>";
    }
    else 
    {
    var image = this.set.replace(/ /g, "_") + '/' + this.rarity + this.number;
    var txt = "";
    txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + ')>";
    txt += this.toString() + "</a>";
    }
    return txt; 
}
Card.prototype.toLink=function()
{
var txt=“”;
这个数字;
如果(this.promo==“false”)
{
var image=this.set.replace(//g,“”)+'/'+this.number;
txt+=”;
txt+=this.toString()+“”;
}
其他的
{
var image=this.set.replace(//g,“”)+'/'+this.rarity+this.number;
var txt=“”;
txt+=”;
txt+=this.toString()+“”;
}
返回txt;
}
下面是我用来填充卡片列表的内容,其中的名称在悬停时将显示一张卡片图像

function populateList () {
    for (i = 0; i<cards.length; i++)
    document.getElementById('myList').innerHTML += '<li>'+cards[i].toLink()+</li>;
}
函数populateList(){
对于(i=0;i
如果我有一个文本html元素,看起来像

<a id='1' onmouseover="changeImage('setname/setnumber')">Cardname</a>
Cardname

我可以在mouseover事件中检索id(在本例中为1),以便在javascript中使用它来执行其他操作

是的,如果您可以更改链接(看起来您可以):

查看您的代码,您需要更新这一行的
toLink

txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + ', this)>";
然后
changeImage
将是:

function changeImage(foo, id) {
    // ...
}
我没有在其周围使用引号,因为这些ID看起来像数字。但如果它不是一个可靠的数字,请使用引号:

txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + ', '" + this.node + "')>";
txt+=”;

好的,老实说,这介于我的位置和我最终想要的位置之间,我曾考虑添加此.node作为第二个参数,但您的回答对我来说会更好,并回答了我的问题。非常感谢。好的,您的回答让我遇到了另一个问题,不知您是否知道导致此问题的原因。
txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + ', " + this.node + ")>";
function changeImage(foo, id) {
    // ...
}
txt += "<a id='" + this.node + "' onmouseover=changeImage('" + image + ', '" + this.node + "')>";