使用document.createElement为MapKit JS标注弹窗添加前缀文本问题
解决MapKit JS标注弹窗电话号码前缀问题
直接通过字符串拼接的方式,给电话号码添加"Phone: "前缀即可,修改代码中设置电话号码文本的行:
修改前:
phone.textContent = annotation.landmark.phone;
修改后(两种写法任选其一):
- 传统字符串拼接:
phone.textContent = "Phone: " + annotation.landmark.phone;
- ES6模板字符串(更简洁):
phone.textContent = `Phone: ${annotation.landmark.phone}`;
完整修改后的代码片段:
// Landmark annotation callout delegate var calloutDelegate = { // Return a div element and populate it with information from the // annotation, including a link to a review site. calloutContentForAnnotation: function(annotation) { var element = document.createElement("div"); element.className = "review-callout-content"; var link = element.appendChild(document.createElement("a")); link.href = annotation.landmark.url; link.textContent = "website"; var phone = element.appendChild(document.createElement("p")); // 这里是修改后的代码 phone.textContent = `Phone: ${annotation.landmark.phone}`; var addLine = element.appendChild(document.createElement("br")); var contact = element.appendChild(document.createElement("a")); contact.href = annotation.landmark.url2; contact.textContent = "contact"; // Add more content. element.style.width = "240px"; element.style.height = "100px"; return element; } }
额外提示(可选):
如果存在电话号码为空的场景,可添加判断避免显示空的"Phone: ":
phone.textContent = annotation.landmark.phone ? `Phone: ${annotation.landmark.phone}` : '';
内容的提问来源于stack exchange,提问作者user982124
相关产品推荐
相关产品推荐

