<ul id="news">
<li><p class="time">2018-10-12</p></li>
<li><p class="time">2017-08-03</p></li>
</ul>
<script>
//网上找的个函数:replacePos(要替换的字符串, 替换第几位, "替换内容");
function replacePos(strObj, pos, replacetext) {
var str = strObj.substr(0, pos-1) + replacetext + strObj.substring(pos, strObj.length);
return str;
}
//修改日期的函数
function picnewsTime() {
var obj = $(".time");//JQ取得所有class为time的标签
for(var i = 0; i < obj.length; i++) {
(function(arg){
var txt = obj[arg].textContent || obj[arg].innerText;//取当前标签内日期值
var newhtml = replacePos(txt, 8, "<strong>");
newhtml += "</strong>";
//这步我就卡住了,onclick去点击时间能实现修改了,但我需要页面加载就修改字符串
obj[arg].onclick = function() {
$(this).html(newhtml);
}
})(i);//传参数i
}
}
picnewsTime();
</script>
人到中年有点甜
相关分类