OGeek|极客世界-中国程序员成长平台

标题: Javascript返回不适合UIWebView给定宽度和高度的内容/字符串 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:22
标题: Javascript返回不适合UIWebView给定宽度和高度的内容/字符串

假设我的 div 中有一些字符串,

“这是我的 html 中的虚拟文本。这只是一个测试”

我会将它加载到 UIWebView,其中我的 webview 宽度 = 100,高度 = 20。现在假设只有 “This is a dummy text inside” 适合 webview。

现在我希望我的 javascript 将剩余的字符串返回给我“我的 html。这只是一个测试”(由于高度较低,不适合)。

在这里我不想调整我的 webview 的大小,我只想要不适合我的 UIWebView 的剩余字符串。

任何建议将不胜感激...

                                     ***I'm newbiew to javascript...



Best Answer-推荐答案


使用Below JS函数..它将返回溢出文本..

示例:

load_content('foo','Sample Text',30,100);


<script type="text/javascript">
    function load_content(div_tag_id,content_string,height,width){

        var text_fit = "";
        var text_overflow = "";
        var string_array = content_string.split(" ");

        for(var i = 0; i < string_array.length; i++ ) {

            document.getElementById(div_tag_id).style.width = width+"px";
            document.getElementById(div_tag_id).innerHTML = document.getElementById(div_tag_id).innerHTML + " "+string_array[i];
            if(document.getElementById(div_tag_id).offsetHeight > height) {
                text_overflow += " "+string_array[i];
            } else {
                text_fit += " "+string_array[i];
            }
        }
        document.getElementById(div_tag_id).innerHTML = text_fit;
        return text_overflow;

    }   
</script>

关于Javascript返回不适合UIWebView给定宽度和高度的内容/字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734559/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4