第 12-22 行
函数openManu
当前在加载时显示一个 block ,并为我的数组创建一个for循环。然后,它的作用是根据数组创建一个 obj 变量,并从调用该函数的链接定义的数组中获取 Manufacturer 。因此,例如,我的 HTML 中的链接将有一个与 openManu('manufacturer')
关联的 onMouseOver
事件,并且 manu 将被定义为制造商,只有“描述”中的“描述”与该制造商对应的数组将被插入到 HTML 中。
我的问题是我正在尝试创建另一个在此之前运行的函数,该函数遍历我的数组并根据数组中的内容创建链接。然后,由于一旦主体加载就会调用此函数,因此链接应该存在,并且链接还将具有 onMouseOver
事件,可以在其中调用第 12-22 行的第二个函数。
目前只有 openManu()
可以在 onMouseOver
上工作,并将根据指定制造商将 Description
插入到我的 HTML 中的对象中 称为内容。我的 createLinks()
函数打开数组并将 URL 定义为变量,并将其插入到创建的链接标记中。
这可以做到吗?顺序是否搞乱了?
javascript:
var arr =
[
{
"Manufacturer": "Any manufacturer",
"Description": "Traditional values",
"URL": "http://www.website.com"
},
{
"Manufacturer": "Different Manufacturer",
"Description": "Short description of said manufacturer",
"URL": "http://www.website2.com"
},
{
"Manufacturer": "A Similar Manufacturer",
"Description": "Not quite the same as the previous manufacturer",
"URL": "http://www.website3.com"
},
{
"Manufacturer": "Manufacturer",
"Description": "Essentially a ripoff of the first manufacturer",
"URL": "http://www.website4.com"
}
]
function createLinks() {
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
URL = obj["URL"];
}
}
document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(\'" ++ m ++ "\')\" onmouseout=\"mouseOut()\" onClick=\"openWeb(\'" ++ URL ++ "\')\">" ++ m ++ "</a>");
}
function openManu(manu) {
document.getElementById('content').style.display = 'block';
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
desc = obj["Description"];
}
}
document.getElementById('content').innerHTML = desc;
}
window.onmousemove = function (e) {
var tooltipSpan = document.getElementById('content');
var x = e.clientX,
y = e.clientY;
tooltipSpan.style.top = (y - 20) + 'px';
tooltipSpan.style.left = x + 'px';
}
var mouseOut = function(){
document.getElementById('content').style.display = 'none'
}
function openWeb(URL) {
window.open(URL);
}
#container{
width:870px;
margin-top:2em;
font-size:1.1em;
position:relative;
padding-left:20px;
display:inline-block;
background-color:#3C3C4E;}
#content{
z-index:1;
display:none;
width:300px;
font-size:16px;
font-family: 'Raleway', sans-serif;
position:absolute;
padding:10px;
background-color:white;}
a {
cursor:pointer;
padding:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
position:inherit;
}
h4 {
padding:0;
z-index:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
font-weight:normal;
font-size:15px;
background-color:#3C3C4E;
position:absolute;
left:8px;
padding:24px;
top:400px;
width:842px;
}
pre {
display:block;
float:left;
line-height:21px;
}
<!DOCTYPE html>
<html onload="createLinks()">
<div id="content"></div>
<pre id="col"></pre>
</html>
旧的 HTML 包含如下所示的链接。
<a onmouseover="openManu('Any manufacturer')" onmouseout="mouseOut()" onClick="openWeb('http://www.website.com/')">Any manufacturer</a>
@Zer00ne 为了回应你的回答,我将 createLinks() 更改为此。我无法让它工作,我可能不完全理解您的解决方案。
function createLinks() {
arr[i]["Manufacturer"]
var obj = arr[i];
var m = obj["Manufacturer"];
document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(\'" ++ m ++ "\')\" onmouseout=\"mouseOut()\" onClick=\"openWeb(\'" ++ URL ++ "\')\">" ++ m ++ "</a>");
}
最佳答案
乍一看,createLinks 函数中的字符串变量串联格式不正确。
function createLinks() {
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
URL = obj["URL"];
}
}
//document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(\'" ++ m ++ "\')\" onmouseout=\"mouseOut()\" onClick=\"openWeb(\'" ++ URL ++ "\')\">" ++ m ++ "</a>");
// should be.
document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(" + m + ") onmouseout=\"mouseOut()\" onClick=\"openWeb(" + URL + ") >" + m + "</a>";
}
// Although m and URL are never defined.
我也不确定您是否正在尝试将变量或字符串传递给 openMenu 函数,但我看到您正在检查 if 语句中的参数。
function openManu(manu) {
document.getElementById('content').style.display = 'block';
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
/*
This will never be true because your argument (manu) comes from whatever you are passing into your function which appears to be a string based on your code. The m that is being assigned from the obj will never be 'm' based on the code. It will be the value that is assigned to the "Manufacturer" key of the object."
*/
desc = obj["Description"];
}
}
document.getElementById('content').innerHTML = desc;
}
我希望这对于正在发生的事情来说是一个良好的开始。
更新——这有效
此代码基于您的 JSFiddle 代码。
HTML
<body onload="createLinks()">
<div id="container">
<pre id="col"></pre>
</div>
<div id="content2"></div>
</body>
CSS
#container{
width:300px;
margin-top:2em;
font-size:1.1em;
position:relative;
display:inline-block;
background-color:#3C3C4E;}
#content{
display:none;
width:300px;
height: 45px;
font-size:16px;
font-family: 'Raleway', sans-serif;
padding:10px;
background-color:white;
}
#col a{
display:block;
padding-left:20px;
}
a {
cursor:pointer;
padding:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
position:inherit;
}
h4 {
padding:0;
z-index:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
font-weight:normal;
font-size:15px;
background-color:#3C3C4E;
position:absolute;
left:8px;
padding:24px;
top:400px;
width:842px;
}
pre {
display:block;
float:left;
line-height:21px;
}
JavaScript
var arr =
[
{
"Manufacturer": "Any manufacturer",
"Description": "Traditional values",
"URL": "http://www.website.com"
},
{
"Manufacturer": "Different Manufacturer",
"Description": "Short description of said manufacturer",
"URL": "http://www.website2.com"
},
{
"Manufacturer": "A Similar Manufacturer",
"Description": "Not quite the same as the previous manufacturer",
"URL": "http://www.website3.com"
},
{
"Manufacturer": "Manufacturer",
"Description": "Essentially a ripoff of the first manufacturer",
"URL": "http://www.website4.com"
}
];
// cache the col element
var col = document.getElementById("col")
// The forEach loop goes through the array
arr.forEach( function(key, idx){ // grab the key an dthe index
var newAchr = document.createElement("a"); // create an anchor DOM element
newAchr.href = key.URL; // assign the href for the element
newAchr.text = key.Manufacturer; // assign the text
newAchr.setAttribute("class", idx); // assign a class that will store the index. This will be used to get the description on mouseover.
document.getElementById('col').appendChild(newAchr); // apend the element to the DOM
});
col.addEventListener("mouseover", function(e){
var target = e.target, //get the currently targeted anchor tag
idx = target.classList[0], // get the index that wsa stored inthe elements class
desc = arr[idx].Description, // get the description from the array usinf the index from the class
contentElem = document.getElementById('content2'); // get a reference to the content element
contentElem.style.display = "block"; // display the content area
contentElem.innerHTML = desc; // add the discription text to the content area
});
col.addEventListener("mouseout", function(e){
var contentElem = document.getElementById('content2'); // get a reference to the content area
contentElem.style.display = "none"; // hide the content area on mouseout
});
关于javascript - 基于数组插入html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834177/