[英]Replace the span text in inner div of same name class
I have a DOM element like 我有一个像
<div class="gm-style-cc">
<div>
<div></div>
<div></div>
</div><span>some text</span>
<div>
<a target="_new">Report a map error</a>
</div>
</div>
<div class="gm-style-cc">
<div>
<span>5000 km</span>
</div>
</div>
I want to replace km to miles in the second span, but I am not even able to access the particular span 我想在第二个跨度中将km替换为英里,但我什至无法访问特定跨度
For testing first I tried to change color , I have tried the following, but not getting exact values 为了进行测试,我尝试更改颜色,我尝试了以下操作,但没有获得确切的值
.gm-style-cc:nth-child(1) span { color: yellow !important; }
.gm-style-cc:nth-child(2) span { color: green !important; }
.gm-style-cc:nth-child(3) span { color: green !important; }
----------
span.gm-style-cc:nth-child(1) { color: yellow !important; }
----------
.gm-style-cc:nth-of-type(1).eq(1) div span{
color: red !important;
}
-------
.gm-style-cc span{
color: red !important;
}
-----------
.gm-style-cc:nth-of-type(1).eq(1) div span{
color: red !important;
}
How can I get the specific position of the span element? 如何获得span元素的特定位置?
Edit : as Mazz suggested 编辑:作为马兹建议
.gm-style-cc div span {
color: red;
}
seems to show changes , but it is doing for all span , i want for span in second class 似乎显示出变化,但它适用于所有跨度,我要在第二类中进行跨度
With jQuery you can do the following. 使用jQuery,您可以执行以下操作。
$('.gm-style-cc div span').css( 'color', 'red' );
Just for the second .gm-style-cc 仅用于第二个.gm-style-cc
$('.gm-style-cc').eq(1).find('div span').css('color', 'red');
This should also work in css 这也应该在CSS中工作
.gm-style-cc div span {
color: red;
}
/* for only the second .gm-style-cc */
.gm-style-cc:eq(2) div span {
color: red;
}
You may have to add !important
if it is from a 3rd party library 如果它来自第三方库,则可能需要添加!important
To change to content: 更改内容:
var sp = $('.gm-style-cc div span');
var value = $('.gm-style-cc div span').text().split(' ');
var newValue = value[0]/1.609344;
sp.text(newValue + ' miles');
You can use basic JavaScript to find your element and replace km
with miles
. 您可以使用基本的JavaScript查找元素,然后将km
替换为miles
。
The following code will search for all span
s that are inside .gm-style-cc
div
s and that contain a number followed by km
in their text, and then do the required replacement: 以下代码将搜索.gm-style-cc
div
且在文本中包含数字后跟km
所有span
,然后进行所需的替换:
// Selecting all spans that are inside .gm-style-cc divs var elms = document.querySelectorAll(".gm-style-cc span"); Array.prototype.forEach.call(elms, function(elm) { // Check if the text of the element contains a number followed by "km" if (elm.innerText.search(/\\d+ km/) !== -1) { // Converting km to miles miles = Number(elm.innerText.split(" ")[0]); miles = miles * 0.621371; // Creating the new contents elm.innerText = String(miles) + " miles"; } })
<div class="gm-style-cc"> <div> <div></div> <div></div> </div><span>some text</span> <div> <a target="_new">Report a map error</a> </div> </div> <div class="gm-style-cc"> <div> <span>5000 km</span> </div> </div>
Try this one [edited] 试试这个[编辑]
(function(){
$('.gm-style-cc').find("span:contains('km')").each(function(i,ele){
var miles = (parseInt($(this).text(),10) / 1.609344) + ' miles';
$(ele).text(miles)
})
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.