我已经获得了一个产品标题,我正在使用这样的javascript拆分和插入换行符:
<script>
function myFunction() {
var str = "How are you - doing today?";
var res = str.split("-").join('<br>');
document.getElementById("demo").innerHTML = res;
}
</script>
这适用于大多数情况,但在某些情况下,我需要完全删除第二行。所以 - 之后的所有内容都需要删除。只是在那个元素中,所以如果我有这个例子
<h3>This is a product - title</h3>
结果应该是
<h3>This is a product</h3>
同样,这只需要应用于具有特定类的元素。有人想知道这件事吗?
答案 0 :(得分:0)
为什么我们不能简单替换,
string = string.replace(/-/g, '<br>');
或完全删除,请
string = string.replace(/-.*$/g, '');
答案 1 :(得分:0)
检查元素的className
:
function myFunction() {
const str = `How are you - doing today?`
const first = str.split(`-`)[0]
const all = str.split(`-`).join(`<br/>`)
const el = document.getElementById(`demo`)
const el.innerHTML = el.className === `any-name` ? first : all
}
答案 2 :(得分:0)
试试这个:
ThreadPoolExecutor executorPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
WorkerThread wt1=new WorkerThread(conn,Queries.getAddressInfo("AL"),oauth_token,restTemplate);
WorkerThread wt2=new WorkerThread(conn,Queries.getAddressInfo("AK"),oauth_token,restTemplate);
executorPool.execute(wt1);
executorPool.execute(wt2);
ScheduledFuture scheduledFuture =
scheduledExecutorService.schedule(new Runnable() {
public void run() {
System.out.println("token service");
String url="";
try {
url = WebServicePropertyFileReader.getOauthUrl()+String.format(urlToGetOauthToken, WebServicePropertyFileReader.getClientId(),
WebServicePropertyFileReader.getClientSecret());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Layer7Token token=restTemplate.postForObject(url, null, Layer7Token.class);
GlobalTokenAccessor.oauth_token=token.getAccessToken();
}
},
50,
TimeUnit.MINUTES);
&#13;
(function() {
// For splitted titles
var split = document.querySelectorAll(".dash-split");
var splits = [];
split.forEach(function(spl) {
splits.push(spl.innerHTML.split("-").join("<br>"));
});
console.log(splits); // Outputs ["This is <br> split!"]
// For removed titles
var removedEls = document.querySelectorAll(".dash-split");
var removed = [];
removedEls.forEach(function(rem) {
removed.push(rem.innerText.split("-")[0].trim());
});
console.log(removed); // Outputs ["This is"]
})();
&#13;
答案 3 :(得分:0)
如果脚本在文档末尾运行,这应该可以满足您的需求。对于包装,它会关闭班级title-wrap
。
<html>
<head></head>
<body>
<h3>This is a product - title</h3>
<h3 class="title title-wrap">This is a product - with a wrapped title</h3>
<h3>This is a product - with another title</h3>
<script>
(function() {
var titles = document.querySelectorAll('h3');
titles.forEach(function(o, i) {
var title = o.innerHTML;
if (/title-wrap/ig.test(o.className)) {
o.innerHTML = title.replace(/-/g, '<br />');
} else {
o.innerHTML = title.replace(/-.*$/g, '');
}
});
})();
</script>
</body>
</html>
&#13;