Javascript 从div中获取所选html文本,并将文本另存为字符串jquery
我有一个div,其中显示了从文本文件读取的数据。现在,我想将所选文本保存到变量中 MyCode:Javascript 从div中获取所选html文本,并将文本另存为字符串jquery,javascript,jquery,html,asp.net-mvc,model-view-controller,Javascript,Jquery,Html,Asp.net Mvc,Model View Controller,我有一个div,其中显示了从文本文件读取的数据。现在,我想将所选文本保存到变量中 MyCode: @section scripts { <script> myHub = $.connection.myHub; $.connection.hub.start() .done(function() { @*console.log("Connected");
@section scripts
{
<script>
myHub = $.connection.myHub;
$.connection.hub.start()
.done(function() {
@*console.log("Connected");
console.log(@Html.Raw(Json.Encode(Model.filePath)));
myHub.server.announce(@Html.Raw(Json.Encode(Model.filePath)));*@
})
.fail(function() {
alert("It Fail");
});
jQuery.get(@Html.Raw(Json.Encode(Model.filePath)),
function(txt) {
$("#LoadText").text(txt);
});
//if (!window.x) {
// x = {};
//}
//x.Selector = {};
//x.Selector.getSelected = function() {
// var t = '';
// if (window.getSelection) {
// t = window.getSelection();
// } else if (document.getSelection) {
// t = document.getSelection();
// } else if (document.selection) {
// t = document.selection.createRange().text;
// }
// return t;
//}
//$(function() {
// $(document).bind("mouseup", function() {
// var mytext = x.Selector.getSelected();
// console.log(mytext.html);
// });
//});
</script>
}
@节脚本
{
myHub=$.connection.myHub;
$.connection.hub.start()
.done(函数(){
@*控制台日志(“已连接”);
log(@Html.Raw(Json.Encode(Model.filePath));
myHub.server.announce(@Html.Raw(Json.Encode(Model.filePath))*@
})
.fail(函数(){
警报(“It故障”);
});
get(@Html.Raw(Json.Encode(Model.filePath)),
函数(txt){
$(“#加载文本”).text(txt);
});
//如果(!window.x){
//x={};
//}
//x、 选择器={};
//x、 Selector.getSelected=函数(){
//var t='';
//if(window.getSelection){
//t=window.getSelection();
//}else if(document.getSelection){
//t=document.getSelection();
//}else if(document.selection){
//t=document.selection.createRange().text;
// }
//返回t;
//}
//$(函数(){
//$(document).bind(“mouseup”,function(){
//var mytext=x.Selector.getSelected();
//log(mytext.html);
// });
//});
}
我尝试了注释代码,但它不起作用,因为它返回
对象,但我需要一个字符串值告诉我们更多信息以帮助您。也许可以尝试在JSFIDLE中提供代码?