Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery在同一类的多个实例中用名字替换全名_Javascript_Jquery - Fatal编程技术网

Javascript jQuery在同一类的多个实例中用名字替换全名

Javascript jQuery在同一类的多个实例中用名字替换全名,javascript,jquery,Javascript,Jquery,类中有全名。标签名有多个实例 <span class="label-name">Joe Demo</span> <span class="label-name">Jane Demo</span> <span class="label-name">Lisa Demo</span> 乔演示 简·德莫 丽莎演示 如何将全名仅替换为第一个名称?您可以获取文本,将其按空格分割,然后获取第一个元素 例如: $( document )

类中有全名。标签名有多个实例

<span class="label-name">Joe Demo</span>
<span class="label-name">Jane Demo</span>
<span class="label-name">Lisa Demo</span>
乔演示 简·德莫 丽莎演示
如何将全名仅替换为第一个名称?

您可以获取文本,将其按空格分割,然后获取第一个元素

例如:

$( document ).ready(function() {
    var fullName = $(".label-name").first().text(); //This will get the text from the first element with the class label-name
    var nameArray = fullName.split(" "); //Split the text by spaces into an array
    var firstName = nameArray[0]; //Take the first element of the array 
    $(".label-name").first().text(firstName); //Set the text of the first element with the class label-name to the first name
});
$( document ).ready(function() {
    var fullNameObjects = $(".label-name");
    $.each(fullNameObjects, function () {
         var nameArray = this.split(" ");
         var firstName = nameArray[0];
         this.text(firstName);
    });
});
编辑:正如我在注释中所指出的,这将获得第一个具有类标签名称的元素。如果该类有多个元素,则可能需要不同的选择器。jQuery选择器记录在。如果你发布完整的HTML源代码,让我知道你想要哪个元素,我会提供进一步的建议

编辑2:如果需要选择并替换同一类的多个元素,可以从代码中删除
.first()
选择器,并使用循环编辑结果。例如:

$( document ).ready(function() {
    var fullName = $(".label-name").first().text(); //This will get the text from the first element with the class label-name
    var nameArray = fullName.split(" "); //Split the text by spaces into an array
    var firstName = nameArray[0]; //Take the first element of the array 
    $(".label-name").first().text(firstName); //Set the text of the first element with the class label-name to the first name
});
$( document ).ready(function() {
    var fullNameObjects = $(".label-name");
    $.each(fullNameObjects, function () {
         var nameArray = this.split(" ");
         var firstName = nameArray[0];
         this.text(firstName);
    });
});

这是一个具有简单解决方案的简单问题。快速的谷歌搜索将提供你需要的任何信息。我的观点是,这个问题应该作为一个重复的问题来解决。显然,有一个愚蠢的问题,我想这就是我所做的。我想如果我不知道如何做某事,但我找不到它,我应该问。我猜不会。如果有几十个标签名称跨度呢?实际上在我的场景中,我确实需要它来处理.label-name.Thank的多个实例,这很有效@J08691不知道我在感谢什么,但不客气:)