简体   繁体   English

在页面上的特定单词后插入换行符

[英]insert line break after a specific word on a page

I am trying to edit this code, 我正在尝试编辑此代码,

I have a list of people on my page in horizontal order, but i would like to insert a linebreak after a specific person's name. 我的页面上有一个水平排列的人员列表,但是我想在特定人员的名字后面插入一个换行符。

I have a div for my line break. 我有一个div换行符。 It's <div class="newline"> 这是<div class="newline">

$(window).load( function(){
  $("#peoplelist p").text("Roger John");
  var html = $(this).html().split(" ");
  html = html[0] + "<br>" + html.slice(1).join(" ");
  $(this).html(html);
});    

but this is not working, can someone help me with this? 但这不起作用,有人可以帮我吗?

If you know the persons name you can just do this, no need for arrays and joining and what not: 如果您知道人员姓名,则可以执行此操作,而无需数组和联接,并且不需要:

Fiddle: http://jsfiddle.net/9CdtL/ 小提琴: http : //jsfiddle.net/9CdtL/


HTML HTML

<div id="peoplelist"><p>Chris Jean, Vic Sen, Roger John, Dan Vany</p></div>

JS JS

$(window).on('load',function(){
    //here I set the name-var to the name I want the linebreak after
    //Now this is quite static, but this could of course also be done using data from a database or something.
    var name = 'Roger';

    //Here I insert the linebreak after the name using 'replace()'
    $('#peoplelist p').html($('#peoplelist p').html().replace(name+' ',name+'<br>'));
});

The main line is this: [string].replace(name+' ', name+'<br>'); 主线是这样的: [string].replace(name+' ', name+'<br>');


EDIT 编辑

I changed the whole answer. 我改变了整个答案。 Sorry, I should have been more clear that the previous answer only showed your result in the Console, nowhere on the page. 抱歉,我应该更清楚地知道上一个答案仅在控制台中显示了您的结果,在页面上没有显示。 It does now. 现在可以了。

BTW , your use of $(this) is wrong, one of the reasons your code isn't working. 顺便说一句 ,您对$(this)是错误的, $(this)是您的代码无法正常工作的原因之一。 You can only use $(this) inside the action-function of an element. 您只能在元素的动作功能内使用$(this)
You are just using it on the next line from the $("#peoplelist p").text("Roger John"); 您只是在$("#peoplelist p").text("Roger John");的下一行使用它$("#peoplelist p").text("Roger John"); line. 线。
Technically, in your case $(this) refers to the window-object, because it's inside the window-load-function. 从技术上讲,在您的情况下, $(this)引用窗口对象,因为它在window-load-function内部。 But that won't work anyway because the window isn't an element on your page that can hold content. 但这无论如何都不会起作用,因为窗口不是页面上可以容纳内容的元素。

It's not working because your $(this) is refering to the window. 它不起作用,因为您的$(this)指向窗口。

You should try this and reference the object directly. 您应该尝试这样做并直接引用该对象。

$(window).load(function(){
    var $listp = $("#peoplelist p")

    $listp.text("Roger John");

    var html = $listp.html().split(" ");
    html = html[0] + "<br>" + html.slice(1).join(" ");
    $listp.html(html);
});

I'm not sure if it's the most elegant solution, but what worked well for me was emptying the text out, and then replacing it via a '.wrapInner()' function (which can incorporate HTML): 我不确定这是否是最优雅的解决方案,但是对我来说最有效的方法是清空文本,然后通过'.wrapInner()'函数(可以合并HTML)将其替换:

$('#peoplelist p').text('');

$('#peoplelist p').wrapInner('Chris<br>Jean<br>Vic<br>Sen<br>Roger<br>John<br>Dan<br>Vany');

http://jsfiddle.net/h1d78sm9/1/ http://jsfiddle.net/h1d78sm9/1/

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

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