获取javascript中超链接后的文本区域内容

Get the textarea content after the hyperlink in javascript

本文关键字:文本 区域 javascript 中超 超链接 获取      更新时间:2023-09-26

我在html文件中有几个文本区域元素(名称相同,没有id),比如: <a href="http://www.google.com" onclick="return foo()">gettext1</a> <textarea name="code" cols="10" rows="30">text1</textarea> <a href="http://www.google.com" onclick="return foo()">gettext2</a> <textarea name="code" cols="10" rows="30">text2</textarea> <a href="http://www.google.com" onclick="return foo()">gettext3</a> <textarea name="code" cols="10" rows="30">text3</textarea>

当用户点击任何链接时,我想获得该链接后面的文本区域的内容。如何在foo方法中访问它?

不使用jquery有可能吗?

function foo(){
//Gather object in variable using javascript getElementById function
var obj = getElementById('code');
//Use javascript .innerhtml function to grab the contents of the element
obj = obj.innerhtml;
//Display text in console to prove working
console.log(obj);
}

你是这个意思吗?

function foo() {
    var comments = $(this).next('textarea').val();
    alert(comments);
    return comments;
}

或者

function foo() {
    var comments = $(this).find("textarea").val();
    alert(comments);
    return comments;
}

function foo() {
    var comments = $("#code").val();
    alert(comments);
    return comments;
}