【发布时间】:2011-07-21 20:50:09
【问题描述】:
我只需要更改标题的第一个值。我做了一些事情,但我不知道这是否是最好的方法。
HTML:
<div title="Title that have to be change"> the div is here </div>
JS:
$("div").click(function(){
var title = $(this).attr('title').split(' ')[0];
/*
title is now getting the first value (Title), so,
i need to change only him.
*/
// this is the better way to do this ?
if(title == "Title"){
$(this).attr("title", "Changed that have to be change");
}
});
我可以只更改标题的一部分,而不是全部更改吗?
【问题讨论】:
-
一个正则表达式可能是有序的。
-
我会使用像
str.replace(/^([^ ])/, "replaced")这样的正则表达式。
标签: javascript jquery