简体   繁体   English

在JavaScript中创建一个可点击的数组

[英]Make a clickable array inside JavaScript

Hey so I have an conundrum that I am trying to resolve. 嘿,所以我有一个难以解决的难题。

So I have a PDB (protein type file) file where I am loading data. 所以我有一个PDB(蛋白质类型文件)文件,我正在加载数据。 I am grabbing the sequence of the protein and then populating a div file with the sequence when the user clicks the button. 我抓住蛋白质的序列,然后在用户单击按钮时用序列填充div文件。

So basically "User Clicks" 所以基本上是“用户点击”

ASDJASDJAKJFSAKDBSKJDBKAJBSDKJFBAKJSBFKJBSKJABFJSABKFAKJBF

And the sequence shows up. 序列出现了。 What I want the user to do is when the user clicks an element inside this sequence it will hightlight the protein accordingly. 我希望用户做的是当用户单击此序列中的元素时,它将相应地高亮显示蛋白质。 The trouble I am having is making the array clickable and trying to figure how to do that. 我遇到的麻烦是使阵列可以点击并试图找到如何做到这一点。

var sequencePdb = [];
document.getElementById("sequence-label").style.visibility = 'visible';
var f = "";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", urlPdb, true);
rawFile.onreadystatechange = function () {

    if(rawFile.readyState === 4) {
        if(rawFile.status === 200 || rawFile.status == 0) {
            f = rawFile.responseText;
            var lines = f.split('\n');

            for (var line = 0; line < lines.length; line++){
               var recordName = lines[line].substr(0, 6);
                var atomName = lines[line].substr(12, 3);
                if (recordName === 'ATOM  ' && atomName === " CA") {
                    var sequenceData = lines[line].substr(17, 3);
                    sequencePDB.push(sequenceDataList[sequenceData]);
                };
            };

            var sequenceLabel = document.getElementById("sequence-label");
            sequenceLabel.innerHTML = sequencePDB.join("");

        };
    };

};

HTML File HTML文件

       <div id = "sequence-label" class="scrollingDiv"  >
        </div>

So far I have tried making each element in an array a clickable item by adding an event listener but it didn't show my alert messages. 到目前为止,我已经尝试通过添加事件监听器使数组中的每个元素成为可点击项,但它没有显示我的警报消息。 So I was wondering if there was a way to make this possible. 所以我想知道是否有办法让这成为可能。 I think the innerHTML converts it into a string so when it reads it, it doesn't pick up the elements inside the array. 我认为innerHTML将它转换为一个字符串,所以当它读取它时,它不会拾取数组中的元素。 So that's where I am stuck. 这就是我被困的地方。

Here's how to make each individual sequence clickable using only one event listener: 以下是如何仅使用一个事件监听器使每个单独的序列可单击:

 var sequencePdb = ["want", "these", "to", "be", "clickable"]; // just pretend we got the data from the XHR (function() { var sequenceLabel = document.getElementById("sequence-label"); // wrap each sequence in a span sequencePdb.forEach(function(pdb) { var span = document.createElement('span'); // use textContent instead of innerHTML to avoid XSS attacks! span.textContent = pdb; sequenceLabel.appendChild(span); }); // only need one event listener sequenceLabel.addEventListener('click', function(event) { // rule out the #sequence-label itself if it was clicked directly if (event.target !== this) { // event.target is span, textContent is string value console.log(event.target.textContent); } }); }()); 
 <div id="sequence-label" class="scrollingDiv"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM