javascript - 如何使用 jquery/Javascript 将段落合并到父链接中?

标签 javascript jquery merge append

我该如何转动它:

<a class="link">
   <p class="paragraph">This is some text</p>
   <p class="paragraph">This is some more text</p>
   <p class="paragraph">And some more</p>
</a>

进入此:

<a class="link">
   This is some text This is some more text And some more
</a>

使用 jQuery。我尝试使用追加和合并,但我就是不明白。

最佳答案

text方法返回元素及其后代的文本内容,您可以使用它:

var link = $("a.link");
link.text(link.text());​​​​​​​​​

来自文档:

Get the combined text contents of each element in the set of matched elements, including their descendants.

这是一个working example .

更新(见评论)

如果需要应用于多个 .link 元素,您可以使用 each:

$("a.link").each(function() {
    $(this).text($(this).text()); 
});

关于javascript - 如何使用 jquery/Javascript 将段落合并到父链接中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10019538/

相关文章:

javascript - 在 iframe 中使用 jQuery $

jquery - 多个 ajax 请求 - jQuery

c - C 中的分而治之范式和递归 - 合并排序示例

javascript - jQuery resize 事件处理程序,在调整大小之前捕获某个元素的原始宽度和高度

javascript - 页面重定向在功能中不起作用

sql - Oracle SQL 根据匹配 ID 更新表

python - 如何为 pd.merge_asof 设置合并和规范化多个数据帧

javascript - ajax 发布到 SharePoint Web 服务因 ntlm 挑战而中断

php - 检查安全 cookie 并在 HTTPS 模式内重定向非 HTTPS 父级

javascript - 为什么这个 javascript 不起作用 - 选择框如果