[英]How do you rearange text in elements using javascript and jquery
I am trying to select all the elements with class "name" and when I click on one radio button it flips the last and first name around, so: Hanks, Tom || 我试图选择所有具有“名称”类的元素,当我单击一个单选按钮时,它会翻转姓氏和名字,因此:Hanks,Tom || Tom, Hanks.
汤姆,汉克斯。 Here is what I have so far:
这是我到目前为止的内容:
HTML: HTML:
<h1>Address Book</h1>
<p>Show Names as:</p>
<input name="lastfirst" value="last" type="radio">First, Last
<input name="firstfirst" value="first" type="radio">Last, First
<div>
<table <thead="">
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</tbody>
<tbody>
<tr>
<td>9001</td>
<td class="name">Tom Hanks,</td>
<td>tomhanks@moviestars.com</td>
</tr>
<tr>
<td>9002</td>
<td class="name">Bruce Willis,</td>
<td>brucewillis@moviestars.com</td>
</tr>
<tr>
<td>9003</td>
<td class="name">Jim Carrey,</td>
<td>jimcarrey@moviestars.com</td>
</tr>
<tr>
<td>9004</td>
<td class="name">Tom Cruise,</td>
<td>tomcruise@moviestars.com</td>
</tr>
<script>
</script>
</tbody>
</table>
</div>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" href="./webassets/style.css" media="screen" type="text/css">
<h1>Company Staff List</h1>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>9001</td>
<td>Tom Hanks,</td>
</tr>
<tr>
<td>9002</td>
<td>Bruce Willis,</td>
</tr>
<tr>
<td>9003</td>
<td>Jim Carrey,</td>
</tr>
<tr>
<td>9004</td>
<td>Tom Cruise,</td>
</tr>
</tbody>
</table>
</div>
Here is my jquery. 这是我的jQuery。 I used a filler of .hide() because other than selecting the elements I am not sure how to do this.
我使用.hide()的填充符是因为除了选择元素外,我不确定如何执行此操作。 Just some hints would help.
只是一些提示会有所帮助。 I am not sure how to separate the first and last name.
我不确定如何区分姓氏和名字。 If I could figure out how to simply swap the first and last name into a variable I could definitely figure out the rest.
如果我能弄清楚如何将名字和姓氏简单地交换成一个变量,我肯定能弄清楚其余的名字。
$(document).ready(function(){
$("input[name='lastfirst']").click(function(){
$(".name").hide();
});
$("input[name='firstfirst']").click(function(){
$(".name").hide();
});
});
$(document).ready(function () {
$("input[name='change_last_first']:nth(0)").prop('checked', true)
$("input[name='change_last_first']").change(function () {
$(".name").text(function (_, old_txt) {
var new_txt = old_txt.split(' ').reverse();
return new_txt.toString().replace(',,', ' ') + ',';
});
});
});
HTML changed HTML已更改
<input name="change_last_first" value="last" type="radio">First, Last
<input name="change_last_first" value="first" type="radio">Last, First
References 参考
Split it first using. 首先使用分割。
var splStr = input.split(/[ ,]+/);
now concatinate using input=splStr[1]+splStr[0];
现在使用
input=splStr[1]+splStr[0];
you can separate the text using split function input.split(/[ ,]+/);
您可以使用拆分功能
input.split(/[ ,]+/);
分隔文本input.split(/[ ,]+/);
along with a regular expression to separate each "word" within the input. 以及用于分隔输入中每个“单词”的正则表达式。 you could then assign each value to a new variable
然后可以将每个值分配给新变量
ex: 例如:
var a = array[0], b = array[1]
From there you could concatenate the array values into any order you choose. 从那里您可以将数组值连接到您选择的任何顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.