[英]Change text of span element when clicking on it using jquery
This is my HTML code:这是我的 HTML 代码:
<div id="trashico">
<img class="deskico" src="images/trashico.png"/>
<p><span class="caption">Trash</span></p>
</div>
Here I want to use jQuery to change the text of the span
when I click on the div
.在这里,我想在单击div
时使用 jQuery 更改span
的文本。 So what jQuery should I have to write?那么我应该写什么 jQuery 呢?
You can do like this你可以这样做
$('#trashico').click(function(){
$(this).find('span').text('your text');
});
write the folowing click event编写以下点击事件
$('#trashico').click(function(){
$('.caption').text('What ever new name you want');
})
Update answer更新答案
As for what you try to achieve is sort of the windows F2 functionality.至于您尝试实现的是 Windows F2 功能。 Try the following logic.试试下面的逻辑。 You would have to implement it using the base idea I've given.您必须使用我给出的基本想法来实现它。
$('#trashico').click(function(){
$(this).addClass('active'); //Add a seperate class to identify on what is clicked
})
$(document).keyup(function(e){
if(e.keycode == 113 && $('.active').length > 0){
$('.caption').text('change name');
}
})
The above code will add a class to the clicked icon on your page.上面的代码将向页面上单击的图标添加一个类。 Sort of selected folder on windows.在 Windows 上对选定文件夹进行排序。 Then there is a keyup event bound to the document where it'll listen to what keys are pressed.然后有一个绑定到文档的 keyup 事件,它将监听按下的键。 when it hits 113 ( is the keycode for F2 ) then the if condition would be true and you would be able to change the name.当它达到 113(是 F2 的键码)时,if 条件为真,您将能够更改名称。
This is the basic logic of the OS renaming function.这是操作系统重命名功能的基本逻辑。 You would have to go through this base to achieve your requirement.您必须通过此基础才能达到您的要求。
$('#trashico').click(function() { $('#trashico > p > span').text("changed text"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="trashico"> <img class="deskico" src="images/trashico.png" /> <p><span class="caption">Trash</span> </p> </div>
Try the following solution.尝试以下解决方案。 Here we are changing the text of span
tag with class caption
:在这里,我们使用类caption
更改span
标签的文本:
$("#trashico").click(function(){
$(this).find(".caption").text('New Text') }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.