【问题标题】:Get next textarea outside div without id获取没有id的div外的下一个textarea
【发布时间】:2013-08-13 13:41:49
【问题描述】:

我需要获取下一个 textarea,但我无法使用 next 甚至 find。

示例代码 HTML:

<div>
    <div class="guides_chapters_button" onclick="surroundtest('[center]', '[/center]');">Center</div>
    <div class="guides_chapters_button" style="text-decoration: underline;" onclick="surround('[u]', '[/u]');">u</div>
</div>
<textarea class="guides_chapters_textarea" id="textareamatch" name="matchupm" rows="7" cols="25"></textarea>

JS:

window.surround = function surround(text2,text3){
$("#textareamatch").surroundSelectedText(text2, text3);
}
function surroundtest(text2,text3){
var c = $(this).parent().next('textarea');
c.surroundSelectedText(text2, text3);
}

JS FIDDLE:http://jsfiddle.net/qmpY8/1/

我需要使用的是环绕测试,另一个是使用 id 的示例。我很想替换那个,因为我正在使用克隆对象。

【问题讨论】:

    标签: javascript textarea next


    【解决方案1】:

    surroundtest 中的 this 语句适用于 window 对象而不是元素。您应该做的是将函数定义更改为:

    function surroundtest(element, text2,text3){
        var c = $(element).parent().next('textarea');
        ...
    }
    

    相应的 HTML:

    <div class="guides_chapters_button" onclick="surroundtest(this, '[center]', '[/center]');">Center</div> 
    

    【讨论】:

    • 如何解决换行问题?这就是我使用另一个窗口的原因。
    【解决方案2】:

    如果这是您要使用的 HTML,那么 .closest() 也可以用于获取 textarea 元素。如下所示:

    var c = $(element).parent().closest('textarea');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多