Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中修剪空白而不是内容?_Javascript_Jquery - Fatal编程技术网

如何在javascript中修剪空白而不是内容?

如何在javascript中修剪空白而不是内容?,javascript,jquery,Javascript,Jquery,所以,我知道javascript可以删减空白,它删减textarea字段中的空白,但删除内容 我想删掉空白并保留字符(帖子内容) App.js var postId = 0; //get data-postid from dashboard article var postBodyElement = null; //for .done - update page without refresh $('.post').find('.interaction').find('.edit').on('

所以,我知道javascript可以删减空白,它删减textarea字段中的空白,但删除内容

我想删掉空白并保留字符(帖子内容)

App.js

var postId = 0; //get data-postid from dashboard article
var postBodyElement = null; //for .done - update page without refresh

$('.post').find('.interaction').find('.edit').on('click', function (event) {

    event.preventDefault();

    postBodyElement = document.getElementById("ed").innerHTML.trim();
    var postBody = postBodyElement.textContent;

    postId = event.target.parentNode.parentNode.dataset['postid']; //get data-postid from dashboard article
    $('#post-body').val(postBody);
    $('#edit-modal').modal();
});

//urlEdit & token are set in the dashboard.blade.php
$('#modal-save').on('click', function () {
    $.ajax({
            method: 'POST',
            url: urlEdit,
            data: {body: $('#post-body').val(),postId: postId, _token: token} //get data-postid from dashboard article
        })
        .done(function (msg) {
            //console.log(msg['message']);
            ///console.log(JSON.stringify(msg));
            $(postBodyElement).text(msg['new_body']); //update page without refresh
            $('#edit-modal').modal('hide'); //hide modal
        });
});
更多参考资料

Dashboard.blade.php

@extends('layouts.layout')
@section('title')
Dashboard
@endsection
@section('content')
<div class="dashboard eli-main">
    <div class="container ">
        <div class="row">
            <div class="col-md-6 col-md-12 push-right">
                <h1>{{$user->username}}</h1>

                <h4>What do you have to say?</h4>
                <form class="make-post">
                    <div class="form-group">
                        <textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
                    </div>
                    <button type="button" id="add" class="mybtn2">Create Post</button>
                    <input type="hidden" value="{{ Session::token() }}" name="_token">
                </form>
                {{ csrf_field() }}


                <article id="owl6" class="post"></article>

                @foreach($posts as $post)
                        <article class="post eli-main" data-postid="{{ $post->id }}">
                         <h3>{{$post->user->username}}</h3>
                            <p id="ed" class="post-bod">{{$post->body}}</p>
                            <div class="info">
                               made on {{ date('F d, Y', strtotime($post->created_at)) }}
                            </div>
                        <div class="interaction">
                            @if(Auth::user() == $post->user)

                                <a href="#" class="edit">Edit</a> |
                                 <a href="{{ route('post.delete', ['post_id' => $post->id]) }}">Delete</a>

                            @endif
                        </div>

                   </article>


                @endforeach




            </div>


        </div>
    </div>
</div>
    <div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h2 class="modal-title">Edit Post</h2>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="post-body">Edit the Post</label>
                            <textarea class="form-control" name="post-body" id="post-body" rows="5"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->



    <script>
        var token = '{{ Session::token() }}';

        var urlEdit = '{{ route('edit') }}';

    </script>
@endsection
@extends('layouts.layout'))
@章节(“标题”)
仪表板
@端部
@节(“内容”)
{{$user->username}
你有什么要说的?
创建帖子
{{csrf_field()}}
|
@恩迪夫
@endforeach
&时代;
编辑文章
编辑帖子
接近
保存更改
var-token='{{Session::token()}}';
var urlEdit='{{route('edit')}}';
@端部

除了使用错误的ID(它是“post body”,而不是“ed”)之外,您还需要获取文本区域的内部HTML,并对其进行修剪–这就是您想要的字符串。但是在下一行中,您尝试从字符串中获取一个属性textContent,它将是未定义的

您应该将这两行改写为:

postBodyElement = document.getElementById("post-body");
var postBody = postBodyElement.value.trim();

注意,我们在这里使用的是value而不是innerHTML,这是获取文本区域值的最佳方法。

document.getElementById(“ed”)
不存在。
textarea
的ID是
post body
。它仍然不起作用,它删除了post内容,而不仅仅是空格。我对post进行了更新,使用完整的仪表板文件,它仍然不会返回textarea内的post内容。