简体   繁体   English

如何使用javascript和jquery重新排列元素中的文本

[英]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();
  });
}); 

DEMO DEMO

$(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 参考

.text() 。文本()

.change() 。更改()

.replace() 。更换()

.split() 。分裂()

.toString() 的ToString()

.reverse() 。相反()

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.

相关问题 您如何在javascript变量中存储带有硒的元素文本? - How do you store an elements text with selenium in a javascript variable? 你如何禁用<script> elements using JavaScript - How do you disable <script> elements using JavaScript 如何使用 JavaScript 在某个元素之后添加元素 - How do you add an element after a certain elements using JavaScript 如何使用javascript / jquery包装带有标签的元素的同级文本? - How to wrap sibling text of elements with tag using javascript/jquery? 您如何使用javascript检测光标是否直接位于文本上方? - How do you detect if the cursor is directly above text using javascript? 如何使用JavaScript将文本框设为只读 - How do you make a text box readonly using JavaScript 如何使用Jquery和JavaScript制作主题切换器? - how do you make a Theme switcher using Jquery and JavaScript? 使用jQuery,如何只找到可见元素并单独留下隐藏元素? - Using jQuery, how do you find only visible elements and leave hidden elements alone? JavaScript/jQuery 你如何 select 所有音频元素动态创建? - JavaScript/jQuery How do you select all Audio Elements created dynamically? 如何在可排序的jqueryUI上触发_rearange方法 - How do I trigger a the _rearange method on a jqueryUI sortable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM