4 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
你可以這樣做:
這會在我們更改文本之前保存 hoved 元素的原始文本,當(dāng)您離開/懸停時(shí),我們會再次插入原始文本。
var original ="";
$('.new-text').hover(
function() {
original = $(this).text();
$(this).text($(this).attr("title"));
}, function() {
$(this).text(original);
}
);
演示
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>

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
你好,這是我的代碼簡單修復(fù)
$(".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>

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果我沒猜錯(cuò),那就試試這個(gè)吧。
是的,您認(rèn)為正確,只有css您必須使用jQuery它是不可能的。試試這個(gè)解決方案。
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>

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
先自己嘗試一次,然后再在這里提出問題,或者如果您這樣做了,請展示您的嘗試
使用 CSS?:hover 選擇器
.new-text:hover {}
使用原生 JS鼠標(biāo)懸停事件
<div onmouseover="()=>{}"></div>
使用 jQuery懸停事件
$(".new-text").hover(()=>{})
添加回答
舉報(bào)