gpt4 book ai didi

javascript - 将字符串切片为两个变量

转载 作者:行者123 更新时间:2023-11-28 13:35:26 25 4
gpt4 key购买 nike

我的属性数据坐标中有坐标

<a class="cor" href="#" data-coordinates="37.650621, 55.740887"></a>

我需要将它们分成两个变量,就像这样

<script>

//this must contain coordinate which is before ","
var firstCor = $('.cor').attr('data-coordinates');

//this must contain coordinate which is after ","
var lastCor = $('.cor').attr('data-coordinates');


</script>

那么,我必须从一个数据坐标得到两个变量

最佳答案

使用.split()

var cor = $('.cor').data('coordinates').split(','); // creates an array of result
var firstCor = cor[0].trim(); // access 1st value index starts from 0
var lastCor = cor[1].trim(); //.trim() to remove empty spaces from start and end

.data()

.trim()

<小时/>更新

对于旧浏览器支持使用

$.trim()而不是.trim()

var firstCor = $.trim(cor[0]);
var lastCor = $.trim(cor[1]);

或使用String.prototype.trim polyfill

关于javascript - 将字符串切片为两个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com