【问题标题】:How do I add a tooltip (based on conditions) to an image in javascript?如何在 javascript 中向图像添加工具提示(基于条件)?
【发布时间】:2013-09-28 12:01:22
【问题描述】:

我在 PHP 文件中找到了根据支持行是否可用来更改实际图像的行,但它没有工具提示并且可能会造成混淆。有人可以帮忙吗?

这是行得通的:

<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>

我试图修改这个:

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

与

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

然后对离线执行相同的操作,但它破坏了代码。

我还在学习,请原谅我的无知。

【问题讨论】:

  • 将标题属性设置为您要显示的字符串

标签: javascript php image wordpress alt


【解决方案1】:

您将无法将其添加到 URL 字符串中并使其正常工作。您需要在脚本标签之间添加额外的 javascript。

您可以考虑使用该变量 $images_url。显然,根据离线/在线状态,that 是变化的变量。您没有包含足够的代码来让我们确切知道如何操作,因为我根本没有看到该变量被设置或操作。

另外一件事——为了显示toollip,你需要改变“title”属性,而不是Alt。但是,您需要使用选择器来选择正确的图像。这是一种方法,通过选择图像的 ID。

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

似乎那里的脚本通过在

中生成javascript来调用图像
<div id='sdFeSX'></div> 

因此,为了显示一个title标签,你需要改变那个div的属性。

当然,您需要在 javascript 中使用某种“if/else”语句。类似于

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

这样,如果将变量 $images_url 设置为在线图像,您的脚本将激活标题标签。只要记住将javascript放在脚本标签中!如果您愿意,可以将其添加到已经存在的脚本末尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 2017-08-11
    • 1970-01-01
    • 2020-07-03
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多