【问题标题】:Changing content of a HTML element's 'title' attribute used for tooltip, using jquery or css使用 jquery 或 css 更改用于工具提示的 HTML 元素的“标题”属性的内容
【发布时间】:2016-11-22 14:12:55
【问题描述】:

我有以下 html 元素:

<a href="#" id="pinterest" alt="PINTEREST">
  <img alt="PINTEREST" title="PINTEREST" src="image.png" class="icon">
</a>

我需要将title 属性值从“pinterest”更改为“联系我们”,当用户将鼠标悬停在上述图片上时,它会用作工具提示。

我已经进行了一些搜索,但我认为它不能用 CSS 完成(除了可能使用 :before:after 伪元素?),所以我现在正在尝试使用 jQuery。

我的 jQuery 看起来像这样:

jQuery(document).ready(function($) {
  $('#pinterest img').attr( 'title', 'Contact Us' );
)};

但它不起作用。

我注意到我的 jQuery 版本是 1.11.3,如果它是相关的(我了解了 prop 用于更高版本)

谢谢。

【问题讨论】:

  • 您的jquery 版本应该无关紧要。您能否确认没有任何控制台错误?
  • 这是一个 wordpress 设置。我看到开发人员工具>控制台显示以下内容:SyntaxError: expected expression, got ')'[Learn More]GET http://.../wp-content/themes/wp_oswad_market-child/images/media/bg-footer.jpg [HTTP/1.1 404 Not Found 1155ms]JQMIGRATE: Migrate is installed, version 1.4.1 jquery-migrate.min.js:2:542 This site appears to use a scroll-linked positioning effect. This may not work well with asynchronous panning; see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects for further details and to join the discussion on related tools and features!
  • 感谢您的建议。就是我用括号交换了花括号,正如您在下面标记的答案中看到的那样。但有趣的是,我没有看到控制台对此给出错误。
  • 您粘贴的上述错误来自console,这意味着您在console 中遇到了该错误。我在为您准备Demo 时发现了这个错误,但我认为您错过了该部分复制粘贴代码时..快乐编码.. :)

标签: jquery html attributes tooltip title


【解决方案1】:

jQuery(document).ready function 有问题,因为您在右括号后添加了卷曲

例如

document.ready 函数以)}; 结尾,而应该以}); 结尾

只需替换:

jQuery(document).ready(function($) {
  $('#pinterest img').attr( 'title', 'Contact Us' );
)};

应该替换为

jQuery(document).ready(function($) {
  $('#pinterest img').attr( 'title', 'Contact Us' );
});

jQuery(document).ready(function($) {
  $('#pinterest img').attr( 'title', 'Contact Us' );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<a href="#" id="pinterest" alt="PINTEREST">
  <img alt="PINTEREST" title="PINTEREST" src="image.png" class="icon">
</a>

注意:与 JQuery 库版本无关 :)

【讨论】:

  • 您的document.ready 函数以)}; 结尾,而应以}); 结尾。这有意义吗??
  • 天哪!为什么是我们忽略的简单事情哈哈。非常感谢你。那是我没有检查的东西。现在可以使用了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多