简体   繁体   English

如何在JavaScript中为图片添加工具提示(基于条件)?

[英]How do I add a tooltip (based on conditions) to an image in javascript?

I've found the line in a PHP file that changes the actual image depending on whether the support line is available or not, but it has no tooltip and can be confusing. 我已经在PHP文件中找到该行,该行会根据支持行是否可用来更改实际图像,但是它没有工具提示,并且可能会造成混淆。 Can someone please help? 有人可以帮忙吗?

This is the line that works: 这是行:

<div id="ciFeSX" style="z-index:100;position:absolute"></div>    
<div id="scFeSX" style="display:inline"></div><div id="sdFeSX" style="display:none"> 
</div>

<script type="text/javascript">
  var seFeSX=document.createElement("script");
  seFeSX.type="text/javascript";
  var seFeSXs=   
           (location.protocol.indexOf("https")==0?"https":"http")+"://image.mysupport.com/js/support/safe-standard.js?ps_h=FeSX&ps_t="+new Date().getTime()+"&online-image={$images_url}livechat_online.png&offline-image={$images_url}livechat_offline.png&ID={$store_id}";

setTimeout("seFeSX.src=seFeSXs;  
document.getElementById('sdFeSX').appendChild(seFeSX)",1)
</script>

<noscript>
<div style="padding:9px;">
<a target="_blank" style="color:#fff;  font-size:20px;"href="http://www.providesupport.com?messenger=propersupport"></a>
</div>
</noscript>

I tried to modify this: 我试图修改此:

 &online-image={$images_url}livechat_online.png

with

&online-image={$images_url}livechat_online.png+image.setAttribute("alt","Live Chat is current ONLINE")

and then do the same for the offline, but it broke the code. 然后对离线进行同样的操作,但是它破坏了代码。

I'm still learning, so please forgive my ignorance. 我仍在学习,所以请原谅我的无知。

You won't be able to just add it into the URL string and have it work. 您将无法将其添加到URL字符串中并使其正常工作。 You need to put additional javascript between script tags. 您需要在脚本标记之间放置其他JavaScript。

What you could consider doing is to use that variable $images_url. 您可以考虑使用变量$ images_url。 Apparently, depending on the offline/online status, that is the variable that changes. 显然,这取决于离线/在线状态, 就是改变变量。 You haven't included enough code to let us know exactly how, since I don't see that variable being set or manipulated at all. 您没有包含足够的代码来让我们确切地知道如何进行操作,因为我根本看不到该变量被设置或操纵。

Another thing - in order to display the toolip, you need to change the "title" attribute, not Alt. 另一件事-为了显示工具提示,您需要更改“标题”属性,而不是Alt。 However, you need to use a selector to select the right image. 但是,您需要使用选择器选择正确的图像。 This is one way, by selecting the image's ID. 这是选择图像ID的一种方法。

document.getElementById('sdFeSX').setAttribute("title","Live Chat is currently ONLINE");

It seems like the script that is there calls the image by generating a javascript within the 似乎那里的脚本通过在

<div id='sdFeSX'></div> 

Therefore, in order to display a title tag, you need to change the attribute of that div. 因此,为了显示标题标签,您需要更改该div的属性。

Of course, you will need some sort of "if/else" statement in javascript. 当然,您需要在javascript中使用某种“ if / else”语句。 Something along the lines of 遵循以下原则

if($images_url == 'something'){
document.getElementById('sdFeSX').setAttribute("title","Live Chat is currently ONLINE");
} 

That way, if the variable $images_url is set to the online image, your script will activate the title tag. 这样,如果将变量$ images_url设置为在线图像,则脚本将激活标题标签。 Just remember to put the javascript in the script tags! 只要记住将javascript放在脚本标签中即可! You can add it to the end of the script already there if you'd like. 您可以根据需要将其添加到脚本的末尾。

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

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