简体   繁体   English

更改按钮文本而无需更改按钮图标

[英]Change button text without button icon change

I have next button: 我有下一个按钮:

 $('.change-button-text').click(function(){ $('.target-chooser').html('Edit target'); }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button"> Choose target <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button"> Change button text </button> 

How change text without losing a button icon? 如何在不丢失按钮图标的情况下更改文本?

You could use a span tag inside of your button: 您可以在按钮内部使用span标签:

 $('.change-button-text').click(function(){ $('.target-chooser span').text('Edit targer'); }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button" data-target-id=""> <span>Choose target</span> <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button" data-target-id=""> Change button text </button> 

or if you don't want to use a span tag: 或者,如果您不想使用span标签:

 $('.change-button-text').click(function(){ $('.target-chooser').html('Edit targer <i class="ace-icon fa fa-arrow-right"></i>'); }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button" data-target-id=""> Choose target <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button" data-target-id=""> Change button text </button> 

The easiest way to do this would be to wrap the text in a span , then target that element. 最简单的方法是将文本包裹在span ,然后将该元素作为目标。

Assuming you can't amend the HTML then you can use contents() and filter() to retrieve the text node, before changing it's textContent . 假设您无法修改HTML,则可以在更改文本节点的textContent之前使用contents()filter()来检索文本节点。 Try this: 尝试这个:

 $('.change-button-text').click(function() { $('.target-chooser').contents().filter(function() { return this.nodeType == 3 && this.textContent.trim(); })[0].textContent = 'Edit target '; }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button"> Choose target <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button"> Change button text </button> 

Add the button text in a span and change the selector to #selector>span like so: 在跨度中添加按钮文本,然后将选择器更改为#selector>span如下所示:

 $('.change-button-text').click(function(){ $('.target-chooser>span').html('Edit targer'); }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button" data-target-id=""> <span>Choose target</span> <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button" data-target-id=""> Change button text </button> 

Or if you do not want to use span use the following: 或者,如果您不想使用跨度,请使用以下命令:

var newText = 'Edit targer';
$('.target-chooser>span').html(newText+'<i class="ace-icon fa fa-arrow-right"></i>');

Try with JavaScript element properties. 尝试使用JavaScript元素属性。 But this is not generic fix. 但这不是通用解决方案。

$('.target-chooser')[0].firstChild.data = 'Edit targer';

Code snippet: 程式码片段:

 $('.change-button-text').click(function(){ $('.target-chooser')[0].firstChild.data = 'Edit targer '; }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-success btn-next target-chooser" type="button"> Choose target <i class="ace-icon fa fa-arrow-right"></i> </button> <button class="change-button-text" type="button"> Change button text </button> 

You can also do it like this 你也可以这样

$(.target-chooser).empty();
$(.target-chooser).append('<i class="ace-icon fa fa-arrow-right"></i> Edit target')

Here you can also change the icon itself 在这里您还可以更改图标本身

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

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