简体   繁体   English

通过将鼠标移到元素上来更改元素的标题

[英]Change the title of an element by moving the mouse over it

I want to write a function called titlep(a) that when the mouse is on the numbers, their title is equal to the product of A in that number.我想写一个名为titlep(a)的 function ,当鼠标在数字上时,它们的标题等于该数字中A的乘积。 (The number A is always changing and the titles change as it changes) (数字A一直在变化,标题随着它的变化而变化)

I want when the mouse is on the numbers +1, +2, +3, +4, the title of these numbers is equal to their product multiplied by the number A.我想当鼠标在数字+1、+2、+3、+4上时,这些数字的标题等于它们的乘积乘以数字A。

Can friends help me where is the problem with Function titlep(a) ?朋友们能帮我看看 Function titlep(a)的问题在哪里吗?

 var number; setInterval(function(){ number = Math.floor((Math.random()*100)+1); document.getElementById("in02").innerHTML = number }, 5000); function titlep(a){ price = number * a this.setAttribute('title', price ); }
 <div style="cursor: pointer"> <span class="tp-cu-po" onmouseover="titlep(4)" title="333">4+ </span><br> <span class="tp-cu-po" onmouseover="titlep(3)">3+ </span><br> <span class="tp-cu-po" onmouseover="titlep(2)">2+ </span><br> <span class="tp-cu-po" onmouseover="titlep(1)">1+ </span><br> </div><br> A = <a id="in02"></a>

Is seemed that you didn't declare any variables with var , const or let so that could be where you were having issues.似乎您没有使用varconstlet声明任何变量,这可能是您遇到问题的地方。

By passing this as a parameter you can access the exactly which span you are hovering.通过this作为参数传递,您可以准确访问您悬停的span This solution you can change the text of any of those span s or even add more and this will still work.此解决方案您可以更改任何这些span的文本,甚至添加更多文本,这仍然有效。

 setInterval(function() { const number = Math.floor((Math.random() * 100) + 1); document.getElementById("in02").innerHTML = number }, 5000); function titlep(elem) { const aVal = document.getElementById('in02').textContent; const elemVal = elem.textContent.replace(/[^0-9]/g, ''); elem.title = Number(aVal) * Number(elemVal); }
 <div style="cursor: pointer"> <span class="tp-cu-po" onmouseover="titlep(this)">4+ </span><br> <span class="tp-cu-po" onmouseover="titlep(this)">3+ </span><br> <span class="tp-cu-po" onmouseover="titlep(this)">2+ </span><br> <span class="tp-cu-po" onmouseover="titlep(this)">1+ </span><br> </div><br> A = <a id="in02"></a>

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

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