查找自定义“分隔线”; >相应地包裹每一侧
我正在寻找一种方法来定位文档中的特定标记,例如 !divide!
或 --divide--
书写语言中通常不存在的东西由正常人类编写,但不需要火箭科学家就能想象出一些东西,而且又与常规文本不同。
然后定位 !divide!
并在删除 !divided!
的同时换行文本。像这样:
Lorem ipsum dolor !divide! sat amet
变为>>
Lorem ipsum dolor sit amet
和
Lorem !划分! ipsum dolor!分裂! sat amet
变为>>
Lorem ipsum dolor sit amet
这是.. 嗯,这就是我到目前为止所拥有的。最重要的是,我试图将我的想法集中在一起,并尝试告诉我我在这里寻找什么: http:// /jsfiddle.net/PvMJL/2/
I'm looking for a way to target a specific marker in a document, like for instance !divide!
or --divide--
Something that doesnt normally exist in language written by normal humanbeings yet something that doesnt take a rocket scientist to conjure and yet again one that is distinct from regular text.
Then target that !divide!
and wrap text while removing !divided!
. Like this:
Lorem ipsum dolor !divide! sit amet
becomes >>
<p>Lorem ipsum dolor</p> <p>sit amet</p>
And
Lorem !divide! ipsum dolor !divide! sit amet
becomes >>
<p>Lorem</p> <p>ipsum dolor</p> <p>sit amet</p>
This is.. Well, this is what i have so far. More than anything, it's me trying to get my thoughts together and try to tell what im looking for here: http://jsfiddle.net/PvMJL/2/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有嵌套
.TextCont
元素,这将避免出现问题。如果这种情况不会发生,您可以摆脱not()
[文档] 方法。示例: http://jsfiddle.net/CFLV4/
我更改了你的 ID到类,因为重复的 ID 无效。
编辑:解释一下,在 JavaScript 中,您不需要附加开始和结束标记。您需要考虑处理整个元素。
在上面的代码中,
html()
[docs]< /i> 方法传递一个函数。函数的返回值是元素的新内容。htm
参数传递一个当前内容的字符串。因此,我们所做的就是获取现有内容,并执行
.split('--divider--')
,生成一个数组,例如:如您所见,
-- divider--
文本已被删除,并且字符串在--divider--
所在位置进行了拆分。然后我们对数组执行
.join()
,使用'
'
作为连接文本,给出:...但是当然,我们缺少开始和结束标记,因此我们将它们添加到开头和结尾,因此我们的代码:
...最终给我们:
这是函数的返回值,因此成为新内容。
This will avoid issues if you have nested
.TextCont
elements. If that won't happen, you can get rid of thenot()
[docs] method.Example: http://jsfiddle.net/CFLV4/
And I changed your IDs to classes since duplicate IDs are invalid.
Edit: To explain, in JavaScript, you don't append opening and closing tags. You need to think in terms of dealing with whole elements.
In the code above, the
html()
[docs] method is passed a function. The return value of the function is the new content of the element. Thehtm
parameter is passed a String of the current content.So what we do is take the existing content, and do a
.split('--divider--')
, resulting in an Array, like:As you can see, the
--divider--
text has been removed, and the string split where the--divider--
was.Then we do a
.join()
on the Array, using'</p><p>'
as the join text, giving us:...but of course we're missing our opening and closing tags, so we add those to the beginning and end, so our code:
...ultimately gives us:
That is the return value of the function, and therefore becomes the new content.
试试这个
Try this