Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 将href链接和id换行到span中的最后3个字符_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将href链接和id换行到span中的最后3个字符

Javascript 将href链接和id换行到span中的最后3个字符,javascript,jquery,html,Javascript,Jquery,Html,我想将href与id一起包装到span中包装的最后三个字符 HTML 最终结果应如下所示: <span id="cartCost">€0.00 <a href="#" id="open">EUR</a></span> 0.00欧元 非常感谢任何解决方案。第一个id必须是唯一的。。改用类 第二: $('.cartCost')。每个(函数(){ //获取文本并使用trim来避免左空格和右空格 var count=$.trim($(this.text

我想将
href
id
一起包装到
span
中包装的最后三个字符

HTML

最终结果应如下所示:

<span id="cartCost">€0.00 <a href="#" id="open">EUR</a></span>
0.00欧元

非常感谢任何解决方案。

第一个id必须是唯一的。。改用类

第二:

$('.cartCost')。每个(函数(){
//获取文本并使用trim来避免左空格和右空格
var count=$.trim($(this.text());
//获取最后三个..子字符串()需要开始索引和结束索引
var lastthree=count.substring(count.length-3,count.length);
//用我们想要的html代码替换最后三个
var replaceit=count.replace(最后三个“”);
//使用.html()将新值附加为html
$(this.html(replaceit);
})

嘿,在拉了一把小提琴之后,这就是我能想到的结果

<script>
    $(document).ready(function(){
        var content = $('#cartCost').html();
        var code  = content.substr(-3);

        var newCode = "<a href='#' id='open'>" + code + "</a>";
        var newContent = content.replace(code, newCode); 
       $('#cartCost').html(newContent);
    });

</script>

<span id="cartCost">€0.00 EUR</span>

$(文档).ready(函数(){
var content=$('#cartCost').html();
var代码=content.substr(-3);
var newCode=“”;
var newContent=content.replace(代码,newCode);
$('#cartCost').html(newContent);
});
0.00欧元

这是

谢谢。在这样的情况下使用class-over-id是一种好的做法吗?还是与此无关?@Panoply始终只对一个元素使用id。。如果使用多个具有相同id的元素,它将只选择其中的第一个。。所以元素的id应该是唯一的。。祝你好运:-)@Panoply额外。。可以对相同的元素使用id和类,如。。抱歉,但我发现我也应该让你知道:-)substr方法是个好主意,但它不适用于ie8或以下版本。那么还有人还在使用ie8吗?2007年就是这样
<span id="cartCost">€0.00 <a href="#" id="open">EUR</a></span>
$('.cartCost').each(function() {
   // get the text and use trim to avoid left and right white spaces
   var count = $.trim($(this).text());
   // get last three .. substring() needs start index and end index 
    var lastthree = count.substring(count.length - 3 , count.length);
   // replace last three with html code we want
    var replaceit = count.replace(lastthree , '<a href="#" class="open">'+ lastthree +'</a>');
   // append the new value as a html using .html() 
    $(this).html(replaceit);
})
<script>
    $(document).ready(function(){
        var content = $('#cartCost').html();
        var code  = content.substr(-3);

        var newCode = "<a href='#' id='open'>" + code + "</a>";
        var newContent = content.replace(code, newCode); 
       $('#cartCost').html(newContent);
    });

</script>

<span id="cartCost">€0.00 EUR</span>