简体   繁体   English

所选文字颜色变化

[英]Selected text color change

i have this style html 我有这种风格的HTML

<p class="paragraph" style="text-indent: 40px" align="justify" id="p5"> <span style="color:black"><a name="art1§2"></a>test<u><sup>o</sup></u> here comes a big text and blah blah lorem ipsum etc....</span>
    <div id="c5" class="spaceforOtherText"></div></p>

im using this function to highlight and get the PARAGRAPH ID(i need it too); 即时通讯使用此功能来突出显示并获取PARAGRAPH ID(我也需要它);

function changecolor(color)  {
  var selection;
  //Get the selected stuff
  if(window.getSelection)
    selection = window.getSelection();
  else if(typeof document.selection!="undefined")
    selection = document.selection;
  //Get a the selected content, in a range object
  var range = selection.getRangeAt(0);
  //If the range spans some text, and inside a tag, set its css class.
  if(range && !selection.isCollapsed)
  {
    if(selection.anchorNode.parentNode == selection.focusNode.parentNode)
    {
      var span = document.createElement('span');
span.style.color =color;
      range.surroundContents(span);
      var x = window.getSelection()
      var z = x.anchorNode.parentNode

      //o que vai ta dentro do p em html
       var html = z.parentNode.innerHTML;
      var id = z.parentNode.id;
       alert(z.parentNode.innerHTML);
     alert(z.parentNode.id);

    Android.save(id, html);

    }
  }

but this code doesnt allow me to edit text color twice, i mean, if i have this text: 但是这段代码不允许我编辑文本颜色两次,我的意思是,如果我有此文本:

<span style"color:red">This is a sample text</span>

and if i want to change "a sample" to blue, i cant, nothing happens, its look like the parent span blocked it, even if the span is inside the text where i want to change color, i cant =/ 如果我想将“样本”更改为蓝色,我不能做任何事情,它看起来像父跨度阻止了它,即使跨度在我要更改颜色的文本内,我也不能= /

its a "problem" in the javascript code? 它是JavaScript代码中的“问题”?

and how can i guarantee that it will return me the , sometimes it return null, i think the z.parentNode its 100% guaranteed 以及我如何保证它将返回我,有时返回null,我认为z.parentNode 100%保证

CSS works in attributes,element concept. CSS在属性,元素概念中起作用。 attributes like class, id, name etc and element like div, span, p etc .. you can change the values with in an element through using attributes.. 属性(例如类,id,名称等)和元素(例如div,span,p等)。您可以通过使用属性来更改元素中的值。

As your problem you can use multiple span for ex - 如果您遇到问题,可以将多个跨度用于ex-

<span style"color:red">This is </span>
<span id='anyid'>a sample</span> 
<span id='anyid'>text</span> 

.. by doing this you can easily change the color for a sample. ..通过此操作,您可以轻松更改样品的颜色。

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

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