【问题标题】:How to get text from an array in javascript and append it to a texarea?如何从javascript中的数组中获取文本并将其附加到textarea?
【发布时间】:2016-10-20 21:45:41
【问题描述】:

我正在尝试从动态更改并存储在值 c 中的数组中获取文件路径。当我单击激活它的按钮时,它会返回文件在数组 exp 中的编号的网址。我想要 www.anon-curb.com/swfs/exampleflash.swf 但它给了我 www.anon-curb.com/swfs/464 ;464 是它在数组中的顺序。

HTML

<div class="share_con">
                <button id="share">Get a link!</button>
                <textarea id="link"></textarea>
            </div>

JAVASCRIPT

    var c = sessionStorage.getItem('order');
    sessionStorage.setItem('order', c);
    var flashcon, test, temp;

    share = document.getElementById('share');
    link = document.getElementById('link');

    function init() {

        share.onclick = function () {
            console.log('Event: share button clicked');
            shareshare();
        }

    }

    function shareshare() {
        console.log('Called: shareshare()');
        link.innerHTML = 'www.anon-curb.com/' + c;
    }

    // What displays the actual flash and title for the flash
    function displayFiles() {
        console.log('Called: displayFiles()');
        test = paths[c].substring(paths[c].lastIndexOf('.') + 1, paths[c].length);
        document.getElementById('title').innerHTML = displaytext[c];

        flashcon.innerHTML =
            '<object id="flashcontent" data="' + paths[c] + '">' +
            '<param name="movie" type="application/x-shockwave-flash">' +
            '</object>';
    }
    window.addEventListener ?
        window.addEventListener('load', init, false) :
        window.attachEvent('onload', init);
});

所有变量都被正确创建和调用我唯一的问题是我不知道如何获取数组中包含的实际文本而不是它的输入顺序。

【问题讨论】:

  • 您的数组可引用为什么?使用括号表示法arr[c]
  • 我提供了更多我认为会有所帮助的 JavaScript 代码
  • 你能给我们调试一下你的sessionStorage吗?
  • 你可能想做link.innerHTML = 'www.anon-curb.com/' + paths[c];而不是link.innerHTML = 'www.anon-curb.com/' + c;。你没有提供你定义paths的代码,但是看看你是怎么用的,我想这就是你需要的吗?
  • 天啊...伙计,我刚刚将其发布为我的答案,但当我这样做时页面没有更新....

标签: javascript jquery arrays html


【解决方案1】:

所以我做了一些摆弄,虽然我有“我太笨了!!!为什么我不直接使用 c 的数字来调用数组中的文本?”所以这就是我所做的!

function shareshare() {
        var l = paths[c];
        console.log('Called: shareshare()');
        link.innerHTML = 'www.anon-curb.com/' + l;
    }

顺便说一句,我不知道为什么我没有立即想到这一点,我觉得 blahhhh

【讨论】:

  • 是的,这就是我在您发布此内容前 2 分钟在 cmets 中提出的建议。
  • Yaaa 我知道,但我没有更新页面,因为我在 choding 时向下滚动,所以我没有看到
  • 没问题。最重要的是你有一个解决方案:)
【解决方案2】:

在查看您的代码时,我发现了一些问题:

  1. 文本区域使用innerText,而不是innerHTML

    这是一个简单的修复,只需在shareshare中切换两个

    function shareshare() {
      console.log('Called: shareshare()');
      link.innerText = 'www.anon-curb.com/' + c;
    }
    

还要记下您的解决方案:

function shareshare() {
    var l = paths[c];
    console.log('Called: shareshare()');
    link.innerHTML = 'www.anon-curb.com/' + l;
}

希望我能对你有所帮助(即使你已经明白了)

【讨论】:

  • 感谢您的回答,但 innerHTML 工作得很好:D
  • 嗯。在我的 Chrome (51) 版本中它不起作用,从我从规范中读到的内容来看,innerText 是使用的方法。直到,你猜!
猜你喜欢
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
相关资源
最近更新 更多