如果我的代码是用LLC硬编码的,则它可以工作。但是,我想传递变量,并且不知道如何修改代码以接受变量。这与《 Zendesk指南》的自定义有关。
对于此行:
https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=LLC',
我想用从HTML传递的变量值替换LLC。
请参见下面的代码:
<div>
{{#if article.labels}}
{{#each article.labels}}
<p>Articles related to <b> {{identifier}} :</b></p>
<ul class="related">
<p id="Project"></p>
</ul>
{{/is}}
{{/each}}
{{/if}}
<script type="text/javascript">
<!-- create elements for LLC articles -->
function createNode(element) {
return document.createElement(element);
}
function append(parent, el) {
return parent.appendChild(el);
}
<!-- list LLC articles-->
const ul = document.getElementById('Project');
const url = 'https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=LLC';
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
let family = data.results;
return family.map(function(article) {
let li = createNode('li'),
span = createNode('span');
span.innerHTML = "<a href ="+`${article.html_url}`+">"+`- ${article.title}`+"</a>";
append(li, span);
append(ul, li);
})
})
.catch(function(error) {
console.log(error);
});
}
</script>
从上面的HTML中,从{{identifier}}
中检索LLC的值。我需要将{{identifier}}
的值传递给JavaScript代码以替换LLC。我该如何实现?
答案 0 :(得分:1)
您拥有LLC
变量,那么这只是简单的字符串连接问题。
const LLC = "something";
const url = 'https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=' + LLC;
如果支持ES6,也可以使用模板字符串文字。
const LLC = "something";
const url = `https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=${LLC}`;