JavaScript将HTML复制到剪贴板解释< > as<和>

时间:2015-10-13 21:43:03

标签: javascript html css

我可以使用JavaScript将DIV的内容复制到剪贴板中,但是HTML标签< &安培; >出来<和>而不是小于或大于符号。任何想法如何解决?链接到小提琴,它似乎不起作用,但在本地工作。 FIDDLE

JS

function copyToClipboard(elementId) {

  // Create a "hidden" input
  var aux = document.createElement("input");

  // Assign it the value of the specified element
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append it to the body
  document.body.appendChild(aux);

  // Highlight its content
  aux.select();

  // Copy the highlighted text
  document.execCommand("copy");

  // Remove it from the body
  document.body.removeChild(aux);

}

HTML

        <div style="display:none" >
    <p id="p1">Content 3 blocks</p></div>

<button onclick="copyToClipboard('p1')">Copy 1 section</button><br/>
<button onclick="copyToClipboard('p2')">Copy 2 section</button><br/>



  <textarea name="textarea" id="p2" cols="45" rows="5"><div class="content-section">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="page-header">
                    <h4 class="pumpkin-txt">Content Section - 4</h4>
                </div>
                <p>Some text here</p>
                <br>
                <h5>More text here</h5>
            </div>
        </div>
    </div>
</div></textarea>

1 个答案:

答案 0 :(得分:0)

这是因为//Just an example, you need to replace all possible offending characters aux.setAttribute( "value", document.getElementById(elementId).innerHTML.replace( /&lt;/g, "<" ) .replace( /&gt;/g, ">" ) .replace( /&quot;/g, "\"" ) ); 正在返回有效的HTML内容。 “&LT;”在节点的值中不是有效的内部内容,因为它是HTML代码。你需要自己更换这些:

DECLARE     @updateCount bigint 
DECLARE     @AnId BIGINT
DECLARE     @aField NVARCHAR(1000)
DECLARE     @NxtID BIGINT

    -- Initialize the update count
    set @updateCount = 0

    DECLARE updatecursor CURSOR FOR
        SELECT id,
         field,
         ORG_ID
            FROM some_table
            WHERE Another_ID IN(SELECT Another_ID FROM Another_Table WHERE NAME='Business_Group') 
    OPEN updatecursor
    FETCH NEXT FROM updatecursor INTO @AnId, @aField,@NxtID
    WHILE @@fetch_status = 0
    BEGIN
        -- Begin transaction for the first record
        if @updateCount = 0
        BEGIN 
            PRINT 'BEGIN TRANSACTION'
            BEGIN TRANSACTION
        END

        PRINT 'UPDATE some_table SET Field='+Char(39)+@aField+Char(39)+' WHERE ID='+CONVERT(VARCHAR,@AnId)+' and Another_ID='+CONVERT(NVARCHAR,@NxtID)+''

        set @updateCount = @updateCount + 1
        -- Commit every 1000 records and start a new transaction
        IF @updateCount % 1000 = 0 
        BEGIN
            PRINT 'COMMIT TRANSACTION'
            PRINT 'WAITFOR DELAY ''00:00:01'''
            -- DONE WITH THE PREVIOUS GROUP, WE NEED THE NEXT
            PRINT 'BEGIN TRANSACTION'
            COMMIT TRANSACTION
            BEGIN TRANSACTION
        END
        FETCH NEXT FROM updatecursor INTO @AnId,@aField,@NxtID
    END
    IF @updateCount % 1000 <> 0
    BEGIN
        -- COMMIT FINAL WHEN TO THE END
        PRINT 'COMMIT TRANSACTION'
        PRINT 'WAITFOR DELAY ''00:00:01'''
        COMMIT TRANSACTION
    END
    CLOSE updatecursor
    DEALLOCATE updatecursor
go

Working JSFiddle