我知道标题可能会令人困惑,但我的问题似乎相当简单。
我正在尝试删除特定字符“#”。
This is how it currently logs (Click Here = Image) 我试图简单地删除“#”字符,这样当它登录到控制台时,文本前面就不会出现#。
我尝试在休息前添加此内容。
var color = stroke.split("#"); 但没有成功。
这是我的代码
xhr = new XMLHttpRequest();
xhr.open("GET", "http://colorillo.com/byjy.inline.svg");
xhr.addEventListener("load", function() {
const xmlDoc = new DOMParser().parseFromString(
this.responseText.trim(),
"image/svg+xml"
);
// Get all polylines as strings
let lines = xmlDoc.getElementsByTagName('polyline');
// Loop over all lines
for(let line of lines) {
// --- OPTIONAL START ---
//See Bruce'es comment (rgb() as output)
let stroke = line.style.stroke;
//let size = line.style.stroke-width; -- Disabled
// --- OR ---
// Loop over all styles of this line (output same as input [hex])
for(let style of line.getAttribute('style').split(';')) {
// Get name & value
let valueOffset = style.indexOf(':');
// Check if name equal to 'stroke'
if(style.substr(0, valueOffset).trim() === 'stroke') {
// Save stroke value
stroke = style.substr(valueOffset + 1).trim();
// Break out of the loop (we don't have to search further)
stroke.split("#"); // Here is what I've tried.
break;
}
}
console.log(stroke)
}
});
xhr.send();
最佳答案
将行程.split("#")更改为
stroke.replace('#','');
这将删除字符串中的第一个“#”。
您可以通过添加来确认
console.log('BEFORE: '+stroke);
在那一行之前,并且
console.log('AFTER: '+stroke);
在它之后。然后在开发工具(或同等工具)中检查 console.log 输出。
关于javascript - 控制台错误,需要删除特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57318587/