简体   繁体   English

悬停时显示标题属性,然后在不悬停时恢复为原始文本

[英]Show title attribute when hovering then revert back to original text when not hovering

When I hover on either of the 'new-text' divs, I want to replace the text in my first div with the title attribute of the div being hovered on, and then revert back to the original text when no longer being hovered on.当我将鼠标悬停在任何一个“新文本”div 上时,我想用悬停在其上的 div 的标题属性替换第一个 div 中的文本,然后在不再悬停时恢复到原始文本。 I don't think this can be done with CSS.我不认为这可以用 CSS 来完成。

The original text and the titles are dynamic variables which will change depending on the page you are on, so any solution needs to not have hard coded values in it.原始文本和标题是动态变量,它们会根据您所在的页面而变化,因此任何解决方案都不需要在其中包含硬编码值。

<div class='changing-text'>Original text</div>

<div class='new-text' title='New text 1'>Div 1</div>

<div class='new-text' title='New text 2'>Div 2</div>

You can do it like this:你可以这样做:

This saved the original text of the hoved element before we change the text and when you leave/hoverout then we insert the original text again.这在我们更改文本之前保存了 hoved 元素的原始文本,当您离开/悬停时,我们再次插入原始文本。

var original ="";

$('.new-text').hover(
  function() {
    original = $(this).text();
    $(this).text($(this).attr("title"));
  }, function() {
    $(this).text(original);
  }
);

Demo演示

 var original =""; $('.new-text').hover( function() { original = $('.changing-text').text(); $('.changing-text').text($(this).attr("title")); }, function() { $('.changing-text').text(original); } );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='changing-text'>Original text</div> <div class='new-text' title='New text 1'>Div 1</div> <div class='new-text' title='New text 2'>Div 2</div>

Hello here is my code simple fix你好,这是我的代码简单修复

 $(".new-text").mouseenter(function() { var title = $(this).attr('title'); var orignal = $('.changing-text').text(); $(this).attr('data-orignal',orignal); $('.changing-text').text(title); }).mouseleave(function() { var orgnl = $(this).attr('data-orignal'); $('.changing-text').text(orgnl); });
 .container div { display: inline-flex; } .new-text { display: block; margin: 15px; cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='changing-text'>Original text</div> <div class="container"> <div class='new-text' title='New text 1'>Div 1</div> <div class='new-text' title='New text 2'>Div 2</div> </div>

If i am getting you right than try this one.如果我说得对,请不要尝试这个。

Yes you think right it is not possible with only css you have to use jQuery for that.是的,您认为正确,仅使用css是不可能的,您必须为此使用jQuery Try this solution.试试这个解决方案。

 jQuery(document).ready(function(){ var changing_text = jQuery('.changing-text').text(); jQuery('.new-text').hover(function(){ var title_value = jQuery(this).attr("title"); jQuery('.changing-text').text(title_value); }, function(){ jQuery('.changing-text').text(changing_text); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='changing-text'>Original text</div> <div class='new-text' title='New text 1'>Div 1</div> <div class='new-text' title='New text 2'>Div 2</div>

Try it once yourself first before throwing a question up here, or show your attempt if you have done so在提出问题之前先自己尝试一次,或者如果您已经这样做了,请展示您的尝试

Using CSS :hover selector使用 CSS :hover 选择器

.new-text:hover {}

Using Native JS mouseover event使用原生 JS鼠标悬停事件

<div onmouseover="()=>{}"></div>

Using jQuery hover event使用 jQuery悬停事件

$(".new-text").hover(()=>{})

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

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